php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #61759 class_alias() should accept classes with leading backslashes
Submitted: 2012-04-18 06:11 UTC Modified: 2013-08-29 08:13 UTC
Votes:2
Avg. Score:3.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (50.0%)
From: aharvey@php.net Assigned: laruence (profile)
Status: Closed Package: Class/Object related
PHP Version: master-Git-2012-04-18 (Git) OS: Irrelevant
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: aharvey@php.net
New email:
PHP Version: OS:

 

 [2012-04-18 06:11 UTC] aharvey@php.net
Description:
------------
Aliasing namespaced classes currently expects that class names will be given in 
the same form as the ZE uses internally; ie without a leading backslash. Since 
that's inconsistent with the absolute form in PHP, it would be good if 
class_alias() could also ignore a leading backslash.

Test script:
---------------
<?php
namespace A;
class C { function foo() { echo "42\n"; } }

namespace B;
class_alias('\A\C', '\B\C');
$c = new C;
$c->foo();

Expected result:
----------------
42

Actual result:
--------------
Fatal error: Class 'B\C' not found in /private/tmp/test.php on line 7

Patches

fix-class_alias (last revision 2013-08-27 09:46 UTC by jpauli@php.net)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-08-26 18:13 UTC] contact at jubianchi dot fr
I experienced the exact same issue on PHP 5.4.17 on OS X 10.9 (Mavericks DP6).
I wrote a simple test case, here it is :

Test script:
---------------
namespace jubianchi\Alias {
    class A {}
 
    var_dump(class_alias('\\jubianchi\\Alias\\A', 'C'));
    $reflector = new \ReflectionClass('C');
    var_dump($reflector->getName());  

    var_dump(class_alias('\\jubianchi\\Alias\\A', '\\jubianchi\\Alias\\B'));
    try {
        $reflector = new \ReflectionClass('\\jubianchi\\Alias\\B');
        var_dump($reflector->getName());
    } catch(\Exception $e) {
        var_dump(get_class($e) . ': ' . $e->getMessage());
    }

    var_dump(class_alias('\\jubianchi\\Alias\\A', 'jubianchi\\Alias\\B'));
    $reflector = new \ReflectionClass('\\jubianchi\\Alias\\B');
    var_dump($reflector->getName());        
}

Expected result:
----------------
bool(true)
string(17) "jubianchi\Alias\A"
bool(true)
string(17) "jubianchi\Alias\A"
bool(true)
string(17) "jubianchi\Alias\A"

Or:
----------------
bool(true)
string(17) "jubianchi\Alias\A"
bool(false)
string(60) "ReflectionException: Class \jubianchi\Alias\B does not exist"
bool(true)
string(17) "jubianchi\Alias\A"

Actual result:
----------------
bool(true)
string(1) "A"
bool(true)
string(17) "jubianchi\Alias\A"
bool(true)
string(60) "ReflectionException: Class \jubianchi\Alias\B does not exist"
bool(true)
string(17) "jubianchi\Alias\A"

As you can see, class_alias returns bool(true) as if everything went fine, so we 
expect the alias to be available but a reflection on the latter throws an 
exception.
I think class_alias should be able to handle the leading backslashes or return 
bool(false) if it can't.
 [2013-08-26 18:32 UTC] johannes@php.net
Note: The bug report is too restrictive. A proper patch would have to work on all places where classnames come from string context. This at first means verifying that all places go via zend_lookup_class() and related functions, not EG(class_table) / CG(class_table)
 [2013-08-27 09:45 UTC] jpauli@php.net
Johannes: I agree, but we could start by patching this bug report right?
I got a patch here : https://github.com/jpauli/php-
src/compare/class_alias_registration_fix
 [2013-08-27 09:46 UTC] jpauli@php.net
The following patch has been added/updated:

Patch Name: fix-class_alias
Revision:   1377596813
URL:        https://bugs.php.net/patch-display.php?bug=61759&patch=fix-class_alias&revision=1377596813
 [2013-08-27 10:08 UTC] johannes@php.net
Technically we could, but it adds some inconsistency if one place allows this but others not and that should be avoided.
 [2013-08-27 10:19 UTC] contact at jubianchi dot fr
I agree with Johannes about consistency.

The severity is not really is not very high and this use case can easily be 
handled at a useland level.

As long as this behavior is not "fixed" I think a warning on the doc shoudl be 
enough, even if I'd like to see it fixed (but as I said, it's not a big deal at 
the moment).

BTW, thanks for you work Julien :)
 [2013-08-27 12:04 UTC] jpauli@php.net
Yep, let's start finding all places where classes as strings can be used, and 
patch them all to use zend_lookup_class().
There shouldn't be tons of them AFAIR.
 [2013-08-27 12:10 UTC] nikic@php.net
I'm not convinced that allowing a leading \ is something we should strive towards. The \ is unnecessary and redundant (as string names are always fully qualified). I'd rather allow only the canonical form.
 [2013-08-27 12:55 UTC] contact at jubianchi dot fr
Also agree with the fact that the leading backslashes are redundant but the point 
is that class_alias returns a value saying all went fine (bool(true)) when the 
alias is not reachable after the call.
 [2013-08-29 07:19 UTC] dmitry@php.net
Automatic comment on behalf of dmitry@zend.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=dfc6feb6e84f27094e6a2e3947caa094f7c35d26
Log: Fixed bug #61759 (class_alias() should accept classes with leading backslashes). (Julien)
 [2013-08-29 07:19 UTC] dmitry@php.net
-Status: Open +Status: Closed
 [2013-08-29 07:19 UTC] dmitry@php.net
Automatic comment on behalf of dmitry@zend.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=15694f66b2732962b760463803a68a1fa3bb098f
Log: Fixed bug #61759 (class_alias() should accept classes with leading backslashes). (Julien)
 [2013-08-29 07:20 UTC] dmitry@php.net
-Status: Closed +Status: Open
 [2013-08-29 07:20 UTC] dmitry@php.net
The fix for this bug has been committed.

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/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.


 [2013-08-29 08:13 UTC] laruence@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: laruence
 [2014-08-22 07:07 UTC] ab@php.net
Automatic comment on behalf of dmitry@zend.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=58606fc10d667ea3330bb3129c2536ce4d714056
Log: Fixed bug #61759 (class_alias() should accept classes with leading backslashes). (Julien)
 [2014-10-07 23:17 UTC] stas@php.net
Automatic comment on behalf of dmitry@zend.com
Revision: http://git.php.net/?p=php-src-security.git;a=commit;h=dfc6feb6e84f27094e6a2e3947caa094f7c35d26
Log: Fixed bug #61759 (class_alias() should accept classes with leading backslashes). (Julien)
 [2014-10-07 23:17 UTC] stas@php.net
Automatic comment on behalf of dmitry@zend.com
Revision: http://git.php.net/?p=php-src-security.git;a=commit;h=15694f66b2732962b760463803a68a1fa3bb098f
Log: Fixed bug #61759 (class_alias() should accept classes with leading backslashes). (Julien)
 [2014-10-07 23:28 UTC] stas@php.net
Automatic comment on behalf of dmitry@zend.com
Revision: http://git.php.net/?p=php-src-security.git;a=commit;h=dfc6feb6e84f27094e6a2e3947caa094f7c35d26
Log: Fixed bug #61759 (class_alias() should accept classes with leading backslashes). (Julien)
 [2014-10-07 23:28 UTC] stas@php.net
Automatic comment on behalf of dmitry@zend.com
Revision: http://git.php.net/?p=php-src-security.git;a=commit;h=15694f66b2732962b760463803a68a1fa3bb098f
Log: Fixed bug #61759 (class_alias() should accept classes with leading backslashes). (Julien)
 [2016-07-20 11:41 UTC] davey@php.net
Automatic comment on behalf of dmitry@zend.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=58606fc10d667ea3330bb3129c2536ce4d714056
Log: Fixed bug #61759 (class_alias() should accept classes with leading backslashes). (Julien)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Feb 01 18:01:29 2025 UTC