Home  >  Article  >  Backend Development  >  Usage of class aliases in PHP 5.3_PHP Tutorial

Usage of class aliases in PHP 5.3_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:44:18759browse

In PHP 5.3, if you want to introduce some classes with very long names, it will be more troublesome to write. At this time,
can use the usage of class aliases in PHP 5.3. Examples are as follows:

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


You can use get_class() to get the original real class name, for example:
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


You can also use alias classes directly in FUNCTION, such as:
class TestClass
{
public function doSomethingWithShortAliasInstance(ShortAlias ​​$b) { }
}
class_alias( Irrational_Long_Class_Name, ShortAlias);
$aInstanceWithAlias ​​= new ShortAlias();
$testClassInstance = new TestClass();
$testClassInstance->doSomethingWithShortAliasInstance($aInstanceWithAlias);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478769.htmlTechArticleIn PHP 5.3, if you want to introduce some classes with long names, it will be more troublesome to write. This time you can The usage of class aliases in PHP 5.3 is as follows: class Irrational_Long...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn