by Andrew Johnstone
In: PHP
10 Dec 2011Every now and then I come across a problem with PHPs autoloading whereby class_exists fails to autoload classes. I’ve noticed this with a number of PHP versions, most recently with 5.3.2-1ubuntu4.9. Typically I’ve resolved this by simply upgrading the PHP version.
In the following example the initial class_exists fails until you instantiate the object and from that point the class exists.
PHP 5.3.2-1ubuntu4.9 with Suhosin-Patch (cli) (built: May 3 2011 00:43:34)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans
This example is simply using Zend Framework + Doctrines Annotations.
var_dump(class_exists('\Everlution\Annotations\Acl', true)); // bool(false)
$obj = new \Everlution\Annotations\Acl(array());
var_dump($obj);
var_dump(class_exists('\Everlution\Annotations\Acl', true)); // bool(true)
bool(false)
object(Everlution\Annotations\Acl)#160 (1) {
["_values":protected]=>
array(0) {
}
}
bool(true)
Andrew Johnstone is a software engineer / lead developer working at Everlution Software.
2 Responses to PHP5.3 autoload/class_exists fail
Mike
June 16th, 2012 at 5:08 am
I had a similar problem I think where a random class would fail to load when others loaded just fine. I switched from the __autoload method to the spl_autoload_register method and the problem cleared up.
andrew.johnstone
June 20th, 2012 at 4:15 pm
In the example above this was in fact using spl_autoload_register.
I’ve never debugged this properly when ever I have encountered this problem.
The typical steps were to attempt to upgrade, clear APC etc.