Description:
------------
When you instantiate a namespaced class, the expected behavior is for the fully qualified namespace with leading slash absent to be passed to your registered function. However, if you instantiate a namespaced class with a class name stored in a variable, the fully qualified namespace is not evaluated and the leading slash (if specified) is included.
You'll have to run the reproduce code twice to see what I mean.
Reproduce code:
---------------
function loadClass($class) {
die($class . PHP_EOL);
}
spl_autoload_register('loadClass');
$myClass = '\Fully\Qualified\ClassName';
// run this first:
new \Fully\Qualified\ClassName();
// run this second:
//new $myClass();
Expected result:
----------------
First run:
Fully\Qualified\ClassName
Second run:
Fully\Qualified\ClassName
Actual result:
--------------
First run:
Fully\Qualified\ClassName
Second run:
\Fully\Qualified\ClassName

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports

[2010-01-12 22:52 UTC] court at epixa dot com

I don't know how I didn't notice this earlier, but it seems the issue isn't simply with a leading slash but with evaluating namespaces entirely for autoloading. Consider the following code coupled with my original autoloader function:
namespace Fully\Qualified;
new ClassName();
// expects: Fully\Qualified\ClassName
// outputs: Fully\Qualified\ClassName
$myClass = 'ClassName';
new $myClass();
// expects: Fully\Qualified\ClassName
// outputs: ClassName
It is my understanding that the loader functions are executed in the global namespace and thus should only be dealing with fully qualified namespaces. It appears as if the fully qualified namespace is evaluated and passed to registered autoloaders if the class name is specified explicitly, but the same cannot be said for class names that are created dynamically.

This bug has been fixed in SVN.
Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
Thank you for the report, and for helping us make PHP better.