php 5.3中的类的别名的用法
可以用PHP 5.3的类的别名的用法,举例说明如下:
class Irrational_Long_Class_Name
{
// empty class
}
class_alias(Irrational_Long_Class_Name, ShortAlias);
$shortAliasInstance = new ShortAlias();
var_dump( $shortAliasInstance instanceof Irrational_Long_Class_Name);
# true
var_dump( $shortAliasInstance instanceof ShortAlias);
# true
可以用get_class()获得原先真实的类名,比如:
class Irrational_Long_Class_Name
{
public function getClass()
{
print get_class();
}
}
class_alias(Irrational_Long_Class_Name, ShortAlias);
$aInstanceWithAlias = new ShortAlias();
$aInstanceWithAlias->getClass();
# Irrational_Long_Class_Name
print get_class($aInstanceWithAlias);
# Irrational_Long_Class_Name
还可以在FUNCTION中,直接使用别名类,比如:
class TestClass
{
public function doSomethingWithShortAliasInstance(ShortAlias $b) { }
}
class_alias(Irrational_Long_Class_Name, ShortAlias);
$aInstanceWithAlias = new ShortAlias();
$testClassInstance = new TestClass();
$testClassInstance->doSomethingWithShortAliasInstance($aInstanceWithAlias);