Last active
December 21, 2015 16:29
-
-
Save jubianchi/6334244 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace { | |
class A {} | |
var_dump(class_alias('A', 'B')); // => bool(true) | |
$reflector = new \ReflectionClass('B'); | |
var_dump($reflector->getName()); // => string(1) "A" | |
} | |
namespace jubianchi\Alias { | |
class A {} | |
var_dump(class_alias('\\jubianchi\\Alias\\A', 'C')); // => bool(true) | |
$reflector = new \ReflectionClass('C'); | |
var_dump($reflector->getName()); // => jubianchi\Alias\A | |
var_dump(class_alias('\\jubianchi\\Alias\\A', '\\jubianchi\\Alias\\B')); // => bool(true) | |
// ^ the leading anti-slashes seem to make the alias fail | |
// but the class_alias call returns true as if everything went fine | |
try { | |
$reflector = new \ReflectionClass('\\jubianchi\\Alias\\B'); | |
var_dump($reflector->getName()); // Expected => jubianchi\Alias\A | |
} catch(\Exception $e) { | |
var_dump(get_class($e) . ': ' . $e->getMessage()); // Got => ReflectionException: Class \jubianchi\Alias\B does not exist | |
} | |
var_dump(class_alias('\\jubianchi\\Alias\\A', 'jubianchi\\Alias\\B')); // => bool(true) | |
$reflector = new \ReflectionClass('\\jubianchi\\Alias\\B'); | |
var_dump($reflector->getName()); // => jubianchi\Alias\A | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ php class_alias.php | |
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" | |
-- Expected -- | |
bool(true) | |
string(1) "A" | |
bool(true) | |
string(17) "jubianchi\Alias\A" | |
bool(true) | |
string(17) "jubianchi\Alias\A" | |
bool(true) | |
string(17) "jubianchi\Alias\A" | |
-- Or expected -- | |
bool(true) | |
string(1) "A" | |
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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment