php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51087 spl_autoload doesn't handle classes starting with T_NS_SEPARATOR properly
Submitted: 2010-02-19 01:17 UTC Modified: 2010-02-19 17:41 UTC
From: andrey at ning dot com Assigned:
Status: Not a bug Package: SPL related
PHP Version: 5.3.2RC2 OS: any
Private report: No CVE-ID:
 [2010-02-19 01:17 UTC] andrey at ning dot com
Description:
------------
When several autoload handlers are registered with spl_autoload_register, and the class starting with T_NS_SEPARATOR (\) AND created using the variable class name is autoloaded, spl_autoload does not stop at the first successful item and goes thru the whole stack.


Reproduce code:
---------------
// Source files:
// foo-bar1.php
namespace foo; class bar1 {}

// foo-bar2.php
namespace foo; class bar2 {}

// foo-baz1.php
namespace foo; class baz1 {}

// foo-baz2.php
namespace foo; class baz2 {}

// test.php
function autoload1($class) {
    echo "autoload1($class) is called!\n";
    $path = array_filter(explode('\\', $class), 'strlen');
    require join('-', $path) . ".php";
}
function autoload2($class) {
    echo "autoload2($class) is called!\n";
    $path = array_filter(explode('\\', $class), 'strlen');
    require join('-', $path) . ".php";
}
spl_autoload_register('autoload1');
spl_autoload_register('autoload2');

$f = new foo\bar1;
$f = new \foo\baz1;
$class = "foo\\bar2"; $f = new $class;
$class = "\\foo\\baz2"; $f = new $class;


Expected result:
----------------
autoload1(foo\bar1) is called!
autoload1(foo\baz1) is called!
autoload1(foo\bar2) is called!
autoload1(\foo\baz2) is called!


Actual result:
--------------
autoload1(foo\bar1) is called!
autoload1(foo\baz1) is called!
autoload1(foo\bar2) is called!
autoload1(\foo\baz2) is called!
autoload2(\foo\baz2) is called!
PHP Fatal error:  Cannot redeclare class foo\baz2


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-02-19 08:31 UTC] jani@php.net
-Status: Open
+Status: Bogus

Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same. 

Thank you for your interest in PHP.

See bug #50731
 [2010-02-19 17:41 UTC] andrey at ning dot com
The issue I'm describing is similar, but different it terms of how spl_autoload handles the cases I'm describing. 
spl_autoload should NOT try going further in the list of autoload handlers when the class is already loaded, but the bug is it considers classes "foo\bar" and "\foo\bar" different and loading "foo\bar" doesn't make "\foo\bar" to appear as loaded.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 16 19:02:26 2014 UTC