php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50731 Inconsistent namespaces sent to functions registered with spl_autoload_register
Submitted: 2010-01-12 18:09 UTC Modified: 2010-03-03 01:16 UTC
Votes:5
Avg. Score:4.4 ± 0.8
Reproduced:4 of 4 (100.0%)
Same Version:3 (75.0%)
Same OS:2 (50.0%)
From: court at epixa dot com Assigned: felipe (profile)
Status: Closed Package: SPL related
PHP Version: 5.3, 6 OS: *
Private report: No CVE-ID: None
 [2010-01-12 18:09 UTC] 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

Patches

Habbo (last revision 2015-05-22 17:13 UTC by gabrielbebe28 at outlook dot com dot br)

Add a Patch

Pull Requests

Add a Pull Request

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.
 [2010-02-19 08:31 UTC] jani@php.net
-Operating System: MAC OS X 10.6.2
+Operating System: *
-PHP Version: 5.3.1
+PHP Version: 5.3, 6

See also bug #51087
 [2010-03-03 01:15 UTC] felipe@php.net
Automatic comment from SVN on behalf of felipe
Revision: http://svn.php.net/viewvc/?view=revision&revision=295763
Log: - Fixed bug #50731 (Inconsistent namespaces sent to functions registered with spl_autoload_register)
 [2010-03-03 01:16 UTC] felipe@php.net
-Status: Open +Status: Closed
 [2010-03-03 01:16 UTC] felipe@php.net
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.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 02:01:28 2024 UTC