Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
[PHP] Easy class & interface loading with automatic initializing
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="MayoMayn" data-source="post: 400131" data-attributes="member: 71840"><p>Well, [USER=9520]@Wess[/USER] you asked and I decided to code it. Could use some changes etc, just gonna post it for now.</p><p><strong>index.php</strong></p><p>[PHP]</p><p>// Require app autoload</p><p>require_once __DIR__ . '/app/autoload_namespace.php';</p><p></p><p>$class = new Syberia\Autoload([</p><p> Syberia\Http\Request::class,</p><p> Syberia\Controller\Home::class</p><p>]);</p><p>[/PHP]</p><p></p><p><strong>autoload_namespace.php</strong></p><p>[PHP]</p><p>namespace Syberia;</p><p></p><p>class Autoload</p><p>{</p><p></p><p> public static $library = [];</p><p></p><p> public function __construct($dir = [])</p><p> {</p><p> if(empty(static::$library)) {</p><p> foreach($dir as $class) {</p><p> // Get the class file</p><p> $name = explode('\\', $class);</p><p> $file = str_replace($name[0], __DIR__ . '/utilities', $class);</p><p> $file = str_replace('\\', '/', $file);</p><p> $file = $file . '.php';</p><p> // Make sure the class is not already declared somewhere else</p><p> if(!array_key_exists($class, get_declared_classes())) {</p><p> // Check if class file exists</p><p> if(file_exists($file)) {</p><p> // Require class</p><p> require_once $file;</p><p> // Check if class file exists</p><p> if(class_exists($class)) {</p><p> // Set class</p><p> static::$library[$class] = new $class();</p><p> } else {</p><p> // Throw error</p><p> die("Unable to load class: " . $class);</p><p> }</p><p> } else {</p><p> // Throw error</p><p> die("Class file {$file} doesn't exist with class " . $class);</p><p> }</p><p> } else {</p><p> // Throw error</p><p> die("Class {$class} already declared.");</p><p> }</p><p> }</p><p> }</p><p> }</p><p></p><p> public static function get($class)</p><p> {</p><p> return static::$library[$class];</p><p> }</p><p></p><p> public static function clear($class = null)</p><p> {</p><p> $class = (!is_null($class) ? static::$library[$class] : static::$library);</p><p> unset($class);</p><p> }</p><p></p><p>}</p><p>[/PHP]</p><p></p><p>EDIT: forgot it wasnt stackoverflow code syntax</p></blockquote><p></p>
[QUOTE="MayoMayn, post: 400131, member: 71840"] Well, [USER=9520]@Wess[/USER] you asked and I decided to code it. Could use some changes etc, just gonna post it for now. [B]index.php[/B] [PHP] // Require app autoload require_once __DIR__ . '/app/autoload_namespace.php'; $class = new Syberia\Autoload([ Syberia\Http\Request::class, Syberia\Controller\Home::class ]); [/PHP] [B]autoload_namespace.php[/B] [PHP] namespace Syberia; class Autoload { public static $library = []; public function __construct($dir = []) { if(empty(static::$library)) { foreach($dir as $class) { // Get the class file $name = explode('\\', $class); $file = str_replace($name[0], __DIR__ . '/utilities', $class); $file = str_replace('\\', '/', $file); $file = $file . '.php'; // Make sure the class is not already declared somewhere else if(!array_key_exists($class, get_declared_classes())) { // Check if class file exists if(file_exists($file)) { // Require class require_once $file; // Check if class file exists if(class_exists($class)) { // Set class static::$library[$class] = new $class(); } else { // Throw error die("Unable to load class: " . $class); } } else { // Throw error die("Class file {$file} doesn't exist with class " . $class); } } else { // Throw error die("Class {$class} already declared."); } } } } public static function get($class) { return static::$library[$class]; } public static function clear($class = null) { $class = (!is_null($class) ? static::$library[$class] : static::$library); unset($class); } } [/PHP] EDIT: forgot it wasnt stackoverflow code syntax [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
[PHP] Easy class & interface loading with automatic initializing
Top