Welcome Guest, Not a member yet? Register   Sign In
Better Autoloading - Extending core/MY_Controller.php
#1

(This post was last modified: 09-01-2015, 11:55 AM by BrendanRehman.)

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.
Reply
#2

Thanks Brendan,
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

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.
Reply
#4

(This post was last modified: 09-02-2015, 10:34 AM by BrendanRehman.)

(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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB