I would like to be able to use OOP and create new objects in my controllers in CodeIgniter. So I need to use an autoload-function:
function __autoload( $classname )
{
require_once("../records/$classname.php");
}
But how can I add that to CodeIgniter?
I would suggest using hooks in order to add this function to your code.
Enable hooks in your config/config.php
In your application/config/hooks.php add new hook on the "pre_system" call, which happens in core/CodeIgniter.php before the whole system runs.
Then in the hooks folder create 2 files:
First: application/hooks/your_functions.php and place your __autoload function and all other functions that you want available at this point.
Second: application/hooks/your_hooks.php and place this code:
This will make all of your functions defined in your_functions.php available everywhere in your code.
You can add your auto loader above to
app/config/config.php
. I've used a similarautoload
function before in this location and it's worked quite neatly.Courtesy of Phil Sturgeon. This way may be more portable.
core
would probably berecords
for you; but check your paths are correct regardless. This method also prevents any interference with loadingCI_
libs (accidentally)the User guide about Auto-loading Resources is pretty cleat about it.