php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49143 is_callable() and unnecessary backslash
Submitted: 2009-08-03 16:19 UTC Modified: 2010-06-11 00:53 UTC
Votes:2
Avg. Score:3.5 ± 1.5
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: david at grudl dot com Assigned: felipe (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.3.0 OS: *
Private report: No CVE-ID: None
 [2009-08-03 16:19 UTC] david at grudl dot com
Description:
------------
is_callable() and method_exists() may invoke autoloader with  unnecessary namespace backslash.

\My\MyClass::func() is the same as My\MyClass::func().





Reproduce code:
---------------
function __autoload($name)
{
	echo $name;
}

is_callable('\My\MyClass::func'); // ERROR

is_callable('My\MyClass::func'); // OK

method_exists('\My\MyClass', 'func'); // ERROR

method_exists('My\MyClass', 'func'); // OK

// defined works well:
defined('\My\MyClass::CONST'); // OK

defined('My\MyClass::CONST'); // OK



Expected result:
----------------
My\MyClass
My\MyClass

My\MyClass
My\MyClass

My\MyClass
My\MyClass

Actual result:
--------------
\My\MyClass
My\MyClass

\My\MyClass
My\MyClass

My\MyClass
My\MyClass

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-08-04 00:34 UTC] david at grudl dot com
Tested with 5.3.1-dev (Sun, 02 Aug 2009 18:53:57 +0000), problem still 
exists.
 [2009-08-04 11:16 UTC] jani@php.net
Yes, which problem..? I don't see any errors. 
 [2009-08-04 11:24 UTC] david at grudl dot com
The two same class invokes autoloader with different class name (maybe 
better term is non-canonicalized class name).

This means: every autoloader must call $name = ltrim($name, "\").

defined() removes leading "\" automatically, is_callable and 
method_exists do not.
 [2010-04-09 08:18 UTC] abca_b_cabcom at hotmail dot com
hi david, you are not in a namespace it is just a classname.
 [2010-06-11 00:48 UTC] alex dot howansky at gmail dot com
This bug is still present in 5.3.2 and also affects instantiating a new class 
from a string variable:

function __autoload($class)
{
    echo "$class\n";
}

$obj = new MyProject\Thing();
// correct: MyProject\Thing

$obj = new \MyProject\Thing();
// correct: MyProject\Thing

$class = '\\MyProject\\Thing';
$obj = new $class();
// wrong: \MyProject\Thing
 [2010-06-11 00:52 UTC] felipe@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: felipe
 [2010-06-11 00:52 UTC] felipe@php.net
This already was fixed for the next release.
See bug #50731

Thanks.
 [2010-06-11 00:53 UTC] felipe@php.net
-Package: Class/Object related +Package: Scripting Engine problem
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 18:01:29 2024 UTC