PHP5.3 autoload/class_exists fail

In: Linux|PHP

10 Dec 2011

Every 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)

2 Responses to PHP5.3 autoload/class_exists fail

Avatar

Mike

July 9th, 2014 at 8:44 pm

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.

Avatar

andrew.johnstone

July 9th, 2014 at 8:45 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.

Comment Form

About this blog

I have been a developer for roughly 10 years and have worked with an extensive range of technologies. Whilst working for relatively small companies, I have worked with all aspects of the development life cycle, which has given me a broad and in-depth experience.