Error loading SPL_AutoLoad PHP5

I have the code below as an autoload class, however it seems that the clean method just doesn't work and always returns to the dirty method.

Am I using spl_autoload incorrectly? If so, what is the correct (best) way? It is inefficient, how can it be improved?

I always get the output, for example, the bottom when using this method, but in some cases it just does not find the class, but does not cause any error. I have display errors set to 1, and checked the error log, but just completely missing.

The code is initialized as

 require "vendor/AutoLoader.class.php"; self::setGlobal("autoloader", AutoLoader::init()); 

And the class is as follows:

 public static $instance; private $_src=array('vendor/', 'lib/', ''); private $_sub=array('base/', ''); private $_ext=array('.php', 'class.php', 'lib.php'); /* initialize the autoloader class */ public static function init(){ if(self::$instance==NULL){ self::$instance=new self(); } return self::$instance; } /* put the custom functions in the autoload register when the class is initialized */ private function __construct(){ spl_autoload_register(array($this, 'clean')); spl_autoload_register(array($this, 'dirty')); } /* the clean method to autoload the class without any includes, works in most cases */ private function clean($class){ $class=str_replace('_', '/', $class); spl_autoload_extensions(implode(',', $this->_ext)); foreach($this->_src as $resource){ foreach($this->_sub as $sub){ echo 'Trying to load ', $class, ' via ', __METHOD__, "()<br />"; set_include_path(pegFramework::getGlobal("baseDir") . $resource.$sub); spl_autoload($class); if(class_exists($class)) { echo 'Found and clean included '.$class.' in '.$resource.$sub."<br />"; break 2; } } } } /* the dirty method to autoload the class after including the php file containing the class */ private function dirty($class){ global $docroot; $class=str_replace('_', '/', $class); foreach($this->_src as $resource){ foreach($this->_ext as $ext){ foreach($this->_sub as $sub){ echo 'Trying to load ', $class, ' via ', __METHOD__, "()<br />"; if(@include(pegFramework::getGlobal("baseDir") . $resource . $sub. $class . $ext)) { echo 'Found and dirty included '.$class.' as '.$resource . $sub. $class . $ext."<br />"; break 3; } } } } spl_autoload($class); } 

 Trying to load pegDatabase via pegAutoloader::clean() ...snip... Trying to load pegDatabase via pegAutoloader::clean() Trying to load pegDatabase via pegAutoloader::dirty() Trying to load pegDatabase via pegAutoloader::dirty() Trying to load basepegDatabase via pegAutoloader::clean() ...snip... Trying to load basepegDatabase via pegAutoloader::clean() Trying to load basepegDatabase via pegAutoloader::dirty() Found and dirty included basepegDatabase as vendor/base/basepegDatabase.php Found and dirty included pegDatabase as vendor/pegDatabase.php Trying to load pegRequest via pegAutoloader::clean() ...snip... Trying to load pegRequest via pegAutoloader::clean() Trying to load pegRequest via pegAutoloader::dirty() Trying to load pegRequest via pegAutoloader::dirty() Found and dirty included pegRequest as vendor/pegRequest.php Trying to load pegFacebook via pegAutoloader::clean() ...snip... Trying to load pegFacebook via pegAutoloader::clean() Trying to load pegFacebook via pegAutoloader::dirty() ...snip... Trying to load pegFacebook via pegAutoloader::dirty() Trying to load Facebook via pegAutoloader::clean() Trying to load Facebook via pegAutoloader::clean() ...snip... Trying to load Facebook via pegAutoloader::dirty() Trying to load Facebook via pegAutoloader::dirty() Trying to load Facebook via pegAutoloader::dirty() ...snip... Trying to load Facebook via pegAutoloader::dirty() Trying to load Facebook via pegAutoloader::dirty() ...snip... Trying to load Facebook via pegAutoloader::dirty() 
+4
source share
1 answer

I think this is a spl_autoload call that disconnects you. In the php documentation comments, I found this :

Note that this function will LOWERCASE the class names it is looking for, do not confuse it if you cannot find Foo_Bar.php

So spl_autoload calls from pegFacebook or Facebook just initiate a search for files named pegfacebook.php or facebook.class.php .

If I were you, I would look at Symfony UniversalClassLoader for examples of more simplified handling of class loading. You could probably configure it to use certain extensions, but I would at least think about finding a new way to deal with this problem.

+2
source

All Articles