**#類原生代碼:**
```
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;
}
```
 
**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>";
```
 
**2.獲取類名**
```
class Person{}
$rf=new ReflectionClass('Person');
echo $rf->getName();
```
 
**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>";
```
 
**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();
?>
```
 
**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>";
?>
```
 
**8.獲取接口**
```
<?php
interface A{}
interface B{}
class C implements A,B{}
$rf=new \ReflectionClass('C');
echo "<pre>";
print_r($rf->getInterfaces());
echo "</pre>";
?>
```
 
**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>";
?>
```
 
**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>";
?>
```
 
**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>";
?>
```
 
**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>";
?>
```
 
**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'))
?>
```
 
**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'))
?>
```
 
**16.檢查類是否是抽象類**
```
<?php
abstract class Person{
abstract function show();
}
$rf=new ReflectionClass('Person');
var_dump($rf->isAbstract())
?>
```
 
**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())
?>
```
 
### **系統的學習PHP**
關注:PHP自學中心,回復相應的關鍵詞,領取以下視頻教程
**全面講解正則表達式【php版】**
公眾號里回復:763641
 
#### **還有其他的教程的關鍵詞,請關注公眾號查看每天分享的文章教程的頭部**

- 第1章:LNP Web環境搭建
- 1-1 Nginx1.19源碼編譯安裝
- 1-2 Nginx1.19環境配置
- 1-3 Nginx1.19性能優化與測試
- 1-4 PHP8.0源碼編譯安裝
- 1-5 PHP8.0環境配置
- 1-6 PHP8.0性能優化與測試
- 第2章:JIT即時編譯
- 2-1 JIT編譯原理
- 2-2 Tracing JIT和Function JIT編譯引擎
- 2-3 Opcodes編譯原理
- 2-4 Opcache和JIT功能開啟
- 2-5 JIT高性能測試
- 第3章:PHP8的主要新特性
- 3-1 php8的命名參數
- 3-2 Reflection反射
- 3-3 注解
- 3-4 構造器屬性提升
- 3-5 聯合類型
- 3-6 Nullsafe空安全運算符
- 3-7 Match表達式
- 第4章:PHP8的新功能和類
- 4-1 PhpToken類
- 4-2 Stringable接口
- 4-3 WeakMap類
- 4-4 Str_contains函數
- 4-5 Str_starts_with和Str_ends_with函數
- 4-6 Fdiv函數
- 4-7 Get_resource_id函數
- 4-8 Get_debug_type函數
- 第5章:類型系統改進
- 5-1 新的Mixed偽類型
- 5-2 Static類方法的返回類型
- 第6章:錯誤處理方面的改進
- 6-1 系統函數引發TypeError和ValueError異常
- 6-2 Throw表達式拋出異常
- 6-3 無變量捕獲的Catch
- 6-4 默認錯誤報告設置為E_ALL
- 6-5 默認情況下顯示PHP啟動錯誤
- 6-6 Assert斷言默認情況下引發異常
- 6-7 操作符@不再抑制Fatal錯誤
- 6-8 PDO默認錯誤模式為ERRMODE_EXCEPTION
- 第7章:資源到對象的遷移
- 7-1 GdImage類對象替換了GD映像資源
- 7-2 CurlHandle類對象替換Curl處理程序
- 7-3 套接字擴展資源Socket是類對象
- 7-4 XMLWriter對象替換xmlwriter資源
- 第8章:PHP面向對象的編程更改
- 8-1 不兼容的方法簽名的致命錯誤
- 8-2 嚴格執行類魔術方法簽名
- 8-3 靜態調用非靜態類方法會導致致命錯誤
- 8-4 繼承規則不適用于Private類方法
- 8-5 對象支持Class魔術常量
- 第9章:與字符串相關的更改
- 9-1 Substr和Iconv_substr偏移越境返回空字符串
- 9-2 加減運算符優先級高于點連接符
- 第10章:其他功能與特性
- 10-1 Printf采用新精度和寬度修飾符
- 10-2 內置Web服務器支持動態端口選擇
- 10-3 參數列表和閉包Use列表中允許結尾逗號
- 10-4 隱式負數組鍵增量不會跳過負數
- 10-5 Crypt函數Salt為必選參數
- 10-6 調用禁用函數或類為未定義狀態
- 10-7 可選參數之后禁止出現必選參數
- 第11章:棄用的函數與方法
- 11-1 ReflectionFunction::isDisabled棄用
- 11-2 ReflectionParameter::getClass棄用
- 11-3 ReflectionParameter::isArray棄用
- 11-4 ReflectionParameter::isCallable棄用
- 11-5 ReflectionClass::export棄用
- 11-6 ReflectionFunction::export棄用
- 11-7 Get_defined_functions改進禁用函數
- 11-8 24個PostgreSQL的別名函數棄用