ID: 50731 Comment by: court at epixa dot com Reported By: court at epixa dot com Status: Open Bug Type: SPL related Operating System: MAC OS X 10.6.2 PHP Version: 5.3.1 New Comment:
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. Previous Comments: ------------------------------------------------------------------------ [2010-01-12 18:09:45] court at epixa dot com 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 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=50731&edit=1