|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
Patchesfix-class_alias (last revision 2013-08-27 09:46 UTC by jpauli@php.net)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 14:00:01 2025 UTC |
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.