Welcome Guest, Not a member yet? Register   Sign In
How to load my classes whenever, wherever without having to include/require them
#1

[eluser]sneakyimp[/eluser]
So it's been awhile since I set up a new CodeIgniter project and I'm wondering one big fat thing: how/where do I create an autoloader function such that my classes will be located correctly without me having to specifically include/require the file that defines them?

I looked at one of my old projects and saw that I had put this code at the end of the /config/config.php file:
Code:
function my_autoload($class) {
$path = array('core', 'libraries', 'models');
if(strpos($class, 'CI_') !== 0) {
  foreach($path as $dir) {
   $full_path = APPPATH . $dir . '/' . $class . '.php';
   if (file_exists($full_path))
    include_once($full_path);
  }
}
}
spl_autoload_register('my_autoload');

A couple of things concern me:
1) This is an odd place to put a function, no? I vaguely recall that it was necessary to define the autoload function sufficiently early that it properly located my classes for use in a controller.
2) If I need to upgrade CodeIgniter, will this be preserved?
3) Is there some other, more 'correct' way to do this?

#2

[eluser]Tpojka[/eluser]
Libraries and models you should set in file:
Code:
APPPATH . 'config/autoload.php'
Core custom made controllers are autoloaded (if followed Phil Sturgeon's method) by function in:
Code:
APPPATH . 'config/config.php'
But also, you can read this great post and make decision where to put some custom initial code.
#3

[eluser]sneakyimp[/eluser]
Thanks for your response!

Quote:Libraries and models you should set in file:
Code:
APPPATH . 'config/autoload.php'
Do you mean to suggest that I must add every class I define to autoload.php? That's pretty onerous, don't you think? I'm likely to define dozens of them.

Phil Sturgeon's method sounds similar to what I've done above, only my autoload function is a bit more elaborate -- and my questions still apply (and need answering Big Grin )

I've examined J.J.'s post and it does seem to impart some general knowledge, but I need more detail to make heads or tails of CodeIgniter's basic function.
#4

[eluser]InsiteFX[/eluser]
Code:
/*
| -------------------------------------------------------------------------
| Native spl_autoload_register() - by Kenneth Vogt
| -------------------------------------------------------------------------
|
| Here is an updated version of Phil Sturgeon’s code:
|
| Thanks to Phil Sturgeon and Kenneth Vogt.
|
| NOTE:
| Requires PHP 5.3.+
| As of CI 3.0 Dev - The constant EXT has been removed modified
| to use '.php' now instead of EXT.
| should work for all version of CI and PHP 5.3
|
| Place at the bottom of your ./application/config/config.php file.
| -------------------------------------------------------------------------
*/
spl_autoload_register(function($class)
{
if (strpos($class, 'CI_') !== 0)
{
  if (file_exists($file = APPPATH . 'core/' . $class . '.php'))
  {
   include $file;
  }
  elseif (file_exists($file = APPPATH . 'libraries/' . $class . '.php'))
  {
   include $file;
  }
}
});

Just add anymore directories that you need.
#5

[eluser]sneakyimp[/eluser]
Thanks for your reply. This code also looks more or less identical to my code. I guess I'll re-post the questions I originally asked to see if maybe someone will bother addressing them:

1) This is an odd place to put a function, no? I vaguely recall that it was necessary to define the autoload function sufficiently early that it properly located my classes for use in a controller.
2) If I need to upgrade CodeIgniter, will this be preserved?
3) Is there some other, more ‘correct’ way to do this?
#6

[eluser]jonez[/eluser]
1) It can go anywhere that's autoloaded. Some people put it in a config file, others in the root index.php. It doesn't really matter as long as you remember where you put it, so when you do upgrade you can check to ensure it carries through.

2) If it's in a config file, as long as you don't overwrite that config file yes it will be preserved.

3) Personal opinion: Yes, don't use autoloading. Since CI doesn't autoload on it's own, and there's probably some code you wrote that loads using CI's normal methods, mixing how you load classes leads to messy unmaintainable code. If the framework autoloads go nuts, if it doesn't don't force it. Consistency is one of the most important aspects of managing a large code base.




Theme © iAndrew 2016 - Forum software by © MyBB