Home > php教程 > php手册 > php 5.3中的类的别名的用法

php 5.3中的类的别名的用法

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 10:42:51
Original
1091 people have browsed it

在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);

Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template