2levelsabove Posted February 13, 2010 Share Posted February 13, 2010 Ok so I know what spl_autoload_register does. Basically you are setting up a structure to where classes get called automatically as they are needed. However I have not seen a workable practical example. Lets say I have an application that can call any number of classes: at the top of the application I put: require_once(dirname(__FILE__) .'/http_autoloader.php'); //helps me autoload classes The contents of my http_autoloader.php are: function __autoload_HTTP_Client($class_name){ $HC = dirname(__FILE__).'/classA.php'; require_once($HC); return true; } spl_autoload_register('__autoload_HTTP_Client'); The problem I am having is that what if my code requires some other class? How do I incorporate that into spl_autoload_register? I just need help understanding it in a simpler fasion. Thanks guys (and yes I have Googled this and the examples are awful) Quote Link to comment Share on other sites More sharing options...
trq Posted February 13, 2010 Share Posted February 13, 2010 Notice how your autoload function needs to accept a classname? You need to use that to load your classes. eg; function __autoload_HTTP_Client($class_name) { $HC = '/path/to/classes' . $class_name . '.php'; return require_once($HC); } spl_autoload_register('__autoload_HTTP_Client'); Now, that's all well and good providing all your classes are within /path/to/classes. However, most people don't want to jam all there classes in one directory like that. Thats when you need to decide on a naming convention that works along side a convention defining where your classes need to be. PEAR has a popular conventional already defined (see here'). Allot of projects, and individuals (including myself) use a similar naming convention. It basically works like this..... Given this directory structure. |-- library | `-- Libname | |-- Application.php | |-- Bootstrap | | `-- Resource | |-- Event | | |-- Chain | | | |-- EventAbstract.php | | | `-- Exception.php | | `-- Chain.php | |-- Exception.php | |-- Loader | | `-- Exception.php | |-- Loader.php | `-- Options.php You keep all your libraries (framework includes) within a defined library directory which is placed on your include path. set_include_path(get_include_path() . PATH_SEPARATOR . '/path/to/library'); Within library you create another directory named after your library. eg; PEAR, Zend (yes, the Zend framework uses the same approach) or Mylib. Now, note the location of the EventAbstract file. Within this file, the EventAbstract class would be named.... abstract class LibName_Event_Chain_EventAbstract Now, with all that in place you can write an autoload function that can easily locate these classes as long as you stick to the convention. set_include_path(get_include_path() . PATH_SEPARATOR . '/path/to/library'); function __autoload_HTTP_Client($class_name) { $class_name = str_replace('_', DIRECTORY_SEPARATOR, $class_name) . '.php'; require_once($HC); } spl_autoload_register('__autoload_HTTP_Client'); Does that help? Quote Link to comment Share on other sites More sharing options...
trq Posted February 13, 2010 Share Posted February 13, 2010 Actually, I should re-iterate over what the __autoload_HTTP_Client function is now doing to $class_name. Its simply replacing _ with / (on linux) or \ (on windows). So if you call.... $foo = new Libname_Event_Chain; __autoload_HTTP_Client receives 'Libname_Event_Chain' changes it to 'Libname/Event/Chain.php' and then includes it. Because the library directory is on your include path the file gets found. Quote Link to comment Share on other sites More sharing options...
2levelsabove Posted February 15, 2010 Author Share Posted February 15, 2010 Thanks so much for the detailed explanation. I really appreciate it. It all makes sense now. cheers! Quote Link to comment Share on other sites More sharing options...
Mchl Posted February 15, 2010 Share Posted February 15, 2010 Also, don't use require, or at least check if file_exists before calling it. Remember that require will result in fatal error when the file cannot be found, which will stop execution of your script and will not give othe autoloading functions (if any) to try to load your class. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.