CodeIgniter Forums
Better Autoloading - Extending core/MY_Controller.php - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Better Autoloading - Extending core/MY_Controller.php (/showthread.php?tid=62836)



Better Autoloading - Extending core/MY_Controller.php - BrendanRehman - 09-01-2015

This is a much better and more modern autoloader... simple instructions follow and you'll be turning your CI app into a literal OOP powerhouse. Pretty much more up to date with PHP 5.3++

http://www.ignitedcoder.com/a-better-autoloader/

Enjoy.


RE: Better Autoloading - Extending core/MY_Controller.php - InsiteFX - 09-01-2015

Thanks Brendan,


RE: Better Autoloading - Extending core/MY_Controller.php - mwhitney - 09-02-2015

It would probably be a good idea to add some code at the beginning of that autoloader to skip loading CI_ prefixed classes (and possibly classes with the configured subclass_prefix) to avoid causing issues when those classes are passed to your autoloader (for instance, when someone calls class_exists() without passing FALSE to the second parameter).

Also, a little trick you can use if you don't intend to use Composer is to set the 'composer_autoload' value in your config file to a path to a script registering your autoloader. Then you don't have to mix configuration and code in your config file.


RE: Better Autoloading - Extending core/MY_Controller.php - BrendanRehman - 09-02-2015

(09-02-2015, 06:37 AM)mwhitney Wrote: It would probably be a good idea to add some code at the beginning of that autoloader to skip loading CI_ prefixed classes (and possibly classes with the configured subclass_prefix) to avoid causing issues when those classes are passed to your autoloader (for instance, when someone calls class_exists() without passing FALSE to the second parameter).

Also, a little trick you can use if you don't intend to use Composer is to set the 'composer_autoload' value in your config file to a path to a script registering your autoloader. Then you don't have to mix configuration and code in your config file.

I'm not married to the code, please feel free to fork the code and submit a PR or pop the snippet of code you want me to add here and I'll gladly add it. I had planned on writing a post for Composer setup and autoloading but perhaps next time... Smile

PS

Thanks for the hint on composer_autoload in config.php, I learned something new today. I was gonna include the autoload file from the vendor folder so this keeps the config file cleaner.

Thanks,
Brendan