I’m writing some unit tests for a class loader, and the first test is there to assert that trying to reference a class without loading it first will indeed fail.
The test looks like this:
/**
* @expectedException PHPUnit_Framework_Error
*/
public function testInstantiateUnloadedClass() {
$foo = new Foo();
}
Sadly, running the test yields this message:
PHP Fatal error: Class ‘Foo’ not found in /Users/jfvaren/workspace/classloader/ClassLoaderTest.php on line 7
Must I simply accept that this is not doable?
Call class_exists(), this tries to autoload the class and returns boolean false if it isn’t able to.
http://php.net/manual/en/function.class-exists.php