多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
**#类原生代码:** ``` namespace php8; class Top{ public function getTop(){ echo 'my name is getTop'; } } trait My{ public function getMy(){ echo 'my name is getMy'; } } /** * @host www.yzmedu.com */ class Person extends Top{ public static $sex='nan'; public $name; public $age; const HOST='www.yzmedu.com'; public function __construct($n,$a){ $this->name=$n; $this->age=$a; } public function say(){ echo "my name is {$this->name},my age is {$this->age}"; } public static function show(){ echo "my name is php8"; } use My; } ``` &emsp; **1.获取类的构造函数** ``` class Person{ public $name; public $age; public function __construct($n,$a){ $this->name=$n; $this->age=$a; } } $rf=new ReflectionClass('Person'); echo "<pre>"; print_r($rf->getConstructor()); echo "</pre>"; ``` &emsp; **2.获取类名** ``` class Person{} $rf=new ReflectionClass('Person'); echo $rf->getName(); ``` &emsp; **3.获取类名的短名** ``` namespace php8; class Person{ public $name; public $age; public function __construct($n,$a){ $this->name=$n; $this->age=$a; } } $rf=new \ReflectionClass('php8\Person'); echo $rf->getShortName(); ``` **4.获取属性** ``` <?php namespace php8; class Person{ public $name; public $age; public function __construct($n,$a){ $this->name=$n; $this->age=$a; } } $rf=new \ReflectionClass('php8\Person'); $arr=$rf->getProperties(); echo "<pre>"; print_r($arr); echo "</pre>"; ?> ``` **5.获取方法** ``` namespace php8; class Person{ public $name; public $age; public function __construct($n,$a){ $this->name=$n; $this->age=$a; } public function say(){ echo "my name is {$this->name},my age is {$this->age}"; } public static function show(){ echo "my name is php8"; } } $rf=new \ReflectionClass('php8\Person'); $arr=$rf->getMethods(); echo "<pre>"; print_r($arr); echo "</pre>"; ``` &emsp; **6.获取命名空间** ``` <?php namespace php8; class Person{ public $name; public $age; public function __construct($n,$a){ $this->name=$n; $this->age=$a; } public function say(){ echo "my name is {$this->name},my age is {$this->age}"; } public static function show(){ echo "my name is php8"; } } $rf=new \ReflectionClass('php8\Person'); echo $rf->getNamespaceName(); ?> ``` &emsp; **7.获取父类** ``` <?php namespace php8; class Top{} class Person extends Top{ public $name; public $age; public function __construct($n,$a){ $this->name=$n; $this->age=$a; } public function say(){ echo "my name is {$this->name},my age is {$this->age}"; } public static function show(){ echo "my name is php8"; } } $rf=new \ReflectionClass('php8\Person'); echo "<pre>"; print_r($rf->getParentClass()); echo "</pre>"; ?> ``` &emsp; **8.获取接口** ``` <?php interface A{} interface B{} class C implements A,B{} $rf=new \ReflectionClass('C'); echo "<pre>"; print_r($rf->getInterfaces()); echo "</pre>"; ?> ``` &emsp; **9.获取静态属性** ``` <?php class Person{ public $name; public $age; static public $sex='nan'; public function __construct($n,$a){ $this->name=$n; $this->age=$a; } public function say(){ echo "my name is {$this->name},my age is {$this->age}"; } public static function show(){ echo "my name is php8"; } } $rf=new ReflectionClass('Person'); echo "<pre>"; print_r($rf->getStaticProperties()); echo "</pre>"; ?> ``` &emsp; **10.获取traits** ``` <?php trait mytrait{ public function gettrait(){ echo "my name gettrait"; } } trait trait2{ public $job; } class Person{ public $name; public $age; static public $sex='nan'; const HOST='www.yzmedu.com'; use trait2; public function __construct($n,$a){ $this->name=$n; $this->age=$a; } public function say(){ echo "my name is {$this->name},my age is {$this->age}"; } public static function show(){ echo "my name is php8"; } use mytrait; } $rf=new ReflectionClass('Person'); echo "<pre>"; print_r($rf->getTraits()); echo "</pre>"; ?> ``` &emsp; **11.获取常量** ``` <?php class Person{ public $name; public $age; static public $sex='nan'; const HOST='www.yzmedu.com'; public function __construct($n,$a){ $this->name=$n; $this->age=$a; } public function say(){ echo "my name is {$this->name},my age is {$this->age}"; } public static function show(){ echo "my name is php8"; } } $rf=new ReflectionClass('Person'); echo "<pre>"; print_r($rf->getConstants()); echo "</pre>"; ?> ``` &emsp; **12.获取文档注释** ``` <?php /** * @host www.yzmedu.com */ class Person{ public $name; public $age; public function __construct($n,$a){ $this->name=$n; $this->age=$a; } public function say(){ echo "my name is {$this->name},my age is {$this->age}"; } public static function show(){ echo "my name is php8"; } } $rf=new ReflectionClass('Person'); echo "<pre>"; print_r($rf->getDocComment()); echo "</pre>"; ?> ``` &emsp; **13.检查常量是否已经定义** ``` <?php class Person{ public $name; public $age; const HOST='www.yzmedu.com'; public function __construct($n,$a){ $this->name=$n; $this->age=$a; } public function say(){ echo "my name is {$this->name},my age is {$this->age}"; } public static function show(){ echo "my name is php8"; } } $rf=new ReflectionClass('Person'); var_dump($rf->hasConstant('HOST')) ?> ``` &emsp; **14.检查方法是否已定义** ``` <?php class Person{ public $name; public $age; const HOST='www.yzmedu.com'; public function __construct($n,$a){ $this->name=$n; $this->age=$a; } public function say(){ echo "my name is {$this->name},my age is {$this->age}"; } public static function show(){ echo "my name is php8"; } } $rf=new ReflectionClass('Person'); var_dump($rf->hasMethod('show')) ?> ``` **15.检查属性是否已定义** ``` <?php class Person{ public $name; public $age; const HOST='www.yzmedu.com'; public function __construct($n,$a){ $this->name=$n; $this->age=$a; } public function say(){ echo "my name is {$this->name},my age is {$this->age}"; } public static function show(){ echo "my name is php8"; } } $rf=new ReflectionClass('Person'); var_dump($rf->hasProperty('name')) ?> ``` &emsp; **16.检查类是否是抽象类** ``` <?php abstract class Person{ abstract function show(); } $rf=new ReflectionClass('Person'); var_dump($rf->isAbstract()) ?> ``` &emsp; **17.检查类是否声明为final** ``` <?php final class Person{ public $name; public $age; const HOST='www.yzmedu.com'; public function __construct($n,$a){ $this->name=$n; $this->age=$a; } public function say(){ echo "my name is {$this->name},my age is {$this->age}"; } public static function show(){ echo "my name is php8"; } } $rf=new ReflectionClass('Person'); var_dump($rf->isFinal()) ?> ``` &emsp; ### **系统的学习PHP** 关注:PHP自学中心,回复相应的关键词,领取以下视频教程 **全面讲解正则表达式【php版】** 公众号里回复:763641 &emsp; #### **还有其他的教程的关键词,请关注公众号查看每天分享的文章教程的头部** ![](https://img.kancloud.cn/96/af/96af322d2cdc53d3fbbe981affa60c7f_150x150.jpg)