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: 399888" data-attributes="member: 71840"><p>Was bored, so coded this with selection of desired directories:</p><p><strong>index.php</strong></p><p>[PHP]</p><p>require_once __DIR__ . '/app/autoload.php';</p><p></p><p>$container = new Container([</p><p> 'dir' => 'utilities',</p><p> 'folders' => [</p><p> 'controllers',</p><p> 'models'</p><p> ]</p><p>]);</p><p></p><p>echo $container->count; // 3</p><p>[/PHP]</p><p><strong>autoload.php</strong></p><p>[PHP]</p><p>class Container</p><p>{</p><p></p><p> public static $library = [];</p><p></p><p> public $count = 1;</p><p></p><p> public function __construct($dir = [])</p><p> {</p><p> if(empty(static::$library)) {</p><p> $folders = implode(",", $dir['folders']);</p><p> // Get all files inside the class folder</p><p> foreach(glob(__DIR__ . "/" . $dir['dir'] . "/{{$folders}}/*.php", GLOB_BRACE) as $file) {</p><p> // Get basename of the file without path</p><p> $base = basename($file);</p><p> // Get name of the file without extension</p><p> $class = substr($base, 0, -4);</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 whether or not interfaces should be included</p><p> if(!isset($dir['interfaces_disabled']) || $dir['interfaces_disabled'] == false) {</p><p> // Get length of file</p><p> $length = strlen($base);</p><p> // Set path for the interface file</p><p> $interface = substr($file, 0, -$length) . "interfaces/".$base;</p><p> // Check if interface file exists</p><p> if(file_exists($interface)) {</p><p> // Set interface</p><p> require_once $interface;</p><p> // Check if interface exists</p><p> if(!interface_exists("Interface".$class)) {</p><p> // Throw error</p><p> die("Unable to load interface: Interface".$class);</p><p> }</p><p> } else {</p><p> // Throw error</p><p> die("Unable to find interface file: " . $interface);</p><p> }</p><p> }</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></p><p> $this->count++;</p><p> } else {</p><p> // Throw error</p><p> die("Unable to load class: " . $class);</p><p> }</p><p></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>}</p><p>[/PHP]</p></blockquote><p></p>
[QUOTE="MayoMayn, post: 399888, member: 71840"] Was bored, so coded this with selection of desired directories: [B]index.php[/B] [PHP] require_once __DIR__ . '/app/autoload.php'; $container = new Container([ 'dir' => 'utilities', 'folders' => [ 'controllers', 'models' ] ]); echo $container->count; // 3 [/PHP] [B]autoload.php[/B] [PHP] class Container { public static $library = []; public $count = 1; public function __construct($dir = []) { if(empty(static::$library)) { $folders = implode(",", $dir['folders']); // Get all files inside the class folder foreach(glob(__DIR__ . "/" . $dir['dir'] . "/{{$folders}}/*.php", GLOB_BRACE) as $file) { // Get basename of the file without path $base = basename($file); // Get name of the file without extension $class = substr($base, 0, -4); // Make sure the class is not already declared somewhere else if(!array_key_exists($class, get_declared_classes())) { // Check whether or not interfaces should be included if(!isset($dir['interfaces_disabled']) || $dir['interfaces_disabled'] == false) { // Get length of file $length = strlen($base); // Set path for the interface file $interface = substr($file, 0, -$length) . "interfaces/".$base; // Check if interface file exists if(file_exists($interface)) { // Set interface require_once $interface; // Check if interface exists if(!interface_exists("Interface".$class)) { // Throw error die("Unable to load interface: Interface".$class); } } else { // Throw error die("Unable to find interface file: " . $interface); } } // Require class require_once $file; // Check if class file exists if(class_exists($class)) { // Set class static::$library[$class] = new $class(); $this->count++; } else { // Throw error die("Unable to load class: " . $class); } } } } } public static function get($class) { return static::$library[$class]; } } [/PHP] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
[PHP] Easy class & interface loading with automatic initializing
Top