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: 399865" data-attributes="member: 71840"><p>Basically a better version of <strong>spl_autoload_register</strong> since it only tries to require the class file whenever you initializes the class.</p><p>Here it automatically gets every file inside your class folder, requires the files and initializes it. All you have to do, is name the class the same as the file.</p><p><strong>index.php</strong></p><p>[PHP]</p><p>// Require loader</p><p>require_once __DIR__ . '/app/autoload.php';</p><p> // Initialize loader</p><p>$loader = new Loader(false);</p><p> // Test</p><p>User::dump(['hello' => 'test']);</p><p>// Get count of how many classes there has been loaded</p><p>echo $loader->count;</p><p>[/PHP]</p><p><strong>autoload.php</strong></p><p>[PHP]</p><p>class Loader</p><p>{</p><p></p><p> private static $library = [];</p><p></p><p> protected static $classPath = __DIR__ . "/classes/";</p><p></p><p> protected static $interfacePath = __DIR__ . "/classes/interfaces/";</p><p></p><p> public $count = 1;</p><p></p><p> public function __construct($requireInterface = true)</p><p> {</p><p> if(empty(static::$library)) {</p><p> // Get all files inside the class folder</p><p> foreach(array_map('basename', glob(static::$classPath . "*.php", GLOB_BRACE)) as $classExt) {</p><p> // Get rid of php extension easily without pathinfo</p><p> $classNoExt = substr($classExt, 0, -4);</p><p> // Make sure the class is not already declared somewhere else</p><p> if(!in_array($classNoExt, get_declared_classes())) {</p><p> $file = static::$classPath . $classExt;</p><p></p><p> if($requireInterface) {</p><p> // Get interface file</p><p> $interface = static::$interfacePath . $classExt;</p><p> // Check if interface file exists</p><p> if(!file_exists($interface)) {</p><p> // Throw exception</p><p> die("Unable to load interface file: " . $interface);</p><p> }</p><p></p><p> // Require interface</p><p> require_once $interface;</p><p> //Check if interface is set</p><p> if(!interface_exists("Interface" . $classNoExt)) {</p><p> // Throw exception</p><p> die("Unable to find interface: " . $interface);</p><p> }</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($classNoExt)) {</p><p> // Set class // class.container.php</p><p> static::$library[$classNoExt] = new $classNoExt();</p><p> $this->count++;</p><p> } else {</p><p> // Throw error</p><p> die("Unable to load class: " . $classNoExt);</p><p> }</p><p></p><p> }</p><p> }</p><p> }</p><p> }</p><p>[/PHP]</p><p>You can easily manage with a bit of coding, to require classes in different folders too.</p><p>Hopefully this can be of some use to any of you.</p></blockquote><p></p>
[QUOTE="MayoMayn, post: 399865, member: 71840"] Basically a better version of [B]spl_autoload_register[/B] since it only tries to require the class file whenever you initializes the class. Here it automatically gets every file inside your class folder, requires the files and initializes it. All you have to do, is name the class the same as the file. [B]index.php[/B] [PHP] // Require loader require_once __DIR__ . '/app/autoload.php'; // Initialize loader $loader = new Loader(false); // Test User::dump(['hello' => 'test']); // Get count of how many classes there has been loaded echo $loader->count; [/PHP] [B]autoload.php[/B] [PHP] class Loader { private static $library = []; protected static $classPath = __DIR__ . "/classes/"; protected static $interfacePath = __DIR__ . "/classes/interfaces/"; public $count = 1; public function __construct($requireInterface = true) { if(empty(static::$library)) { // Get all files inside the class folder foreach(array_map('basename', glob(static::$classPath . "*.php", GLOB_BRACE)) as $classExt) { // Get rid of php extension easily without pathinfo $classNoExt = substr($classExt, 0, -4); // Make sure the class is not already declared somewhere else if(!in_array($classNoExt, get_declared_classes())) { $file = static::$classPath . $classExt; if($requireInterface) { // Get interface file $interface = static::$interfacePath . $classExt; // Check if interface file exists if(!file_exists($interface)) { // Throw exception die("Unable to load interface file: " . $interface); } // Require interface require_once $interface; //Check if interface is set if(!interface_exists("Interface" . $classNoExt)) { // Throw exception die("Unable to find interface: " . $interface); } } // Require class require_once $file; // Check if class file exists if(class_exists($classNoExt)) { // Set class // class.container.php static::$library[$classNoExt] = new $classNoExt(); $this->count++; } else { // Throw error die("Unable to load class: " . $classNoExt); } } } } } [/PHP] You can easily manage with a bit of coding, to require classes in different folders too. Hopefully this can be of some use to any of you. [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
[PHP] Easy class & interface loading with automatic initializing
Top