**一.注釋定義**
```
/**
* @api http://www.yzmedu.com/api
*/
function show($name){}
```
 
**二.獲取注釋**
```
$ref=new ReflectionFunction("show");
$doc=$ref->getDocComment();
$api=substr($doc, strpos($doc, "@api") + strlen("@api "),-2);
```
 
**三.Attributes語法**
```
#[Name]
#[Name1,Name2]
#[Name(Arguments)]
#[Name(Argunment1,Arguments2,ArgumentN)]
#[Name1(Argument),Name2(Argument),Name3(Argument)]
```
 
**四.注解定義**
1.單參數
```
#[api("http://www.yzmedu.com/api")]
function show($name){}
```
2.多參數
```
#[myattr("api","http://www.yzmedu.com/api")]
#[api("http://www.yzmedu.com/api")]
```
 
**五.獲取注解**
```
$ref=new ReflectionFunction("show");
$attr=$ref->getAttributes("api")[0];
$name=$attr->getName();
$value=$attr->getArguments();
```
 
**六.ReflectionAttribute類結構**
```
final class ReflectionAttribute {
/**
* @return string The name of the attribute, with class names resolved.
*/
public function getName(): string {}
/**
* @return array Arguments passed to the attribute when it is declared.
*/
public function getArguments(): array {}
/**
* @return object An instantiated class object of the name, with arguments passed to the constructor.
*/
public function newInstance(): object {}
}
```
 
**七.函數注解類使用**
```
#[Attribute(Attribute::TARGET_FUNCTION)]
class MyAttr{
public function __construct($name, $value) {
echo "$name,$value";
}
}
#[MyAttr("api","http://www.yzmedu.com/api")]
function show(){}
$ref=new ReflectionFunction("show");
$attr=$ref->getAttributes("MyAttr")[0]->newInstance();
```
 
**八.注解類類型**
```
TARGET_CLASS //類的注解類
TARGET_FUNCTION //函數注解類
TARGET_METHOD //方法注解類
TARGET_PROPERTY //屬性注解類
TARGET_CLASS_CONSTANT //類常量注解類
TARGET_PARAMETER //參數注解類
TARGET_ALL
```
 
**九.函數注解類高級配置**
```
#[Attribute(Attribute::TARGET_FUNCTION)]
class Myattr{
public $name;
public $value;
public $number;
public function __construct($name,$value,$number){
$this->name=$name;
$this->value=$value;
$this->number=$number;
}
public function say(){
echo "{$this->name}--{$this->value}--{$this->number}";
}
}
#[Myattr('api','http://www.yzmedu.com/api','10')]
function show(){}
$ref=new ReflectionFunction("show");
$attr=$ref->getAttributes('Myattr')[0];
$obj=$attr->newInstance();
$obj->say();
```
 
**十.類注解類使用**
```
#[Attribute(Attribute::TARGET_CLASS)]
class MyAttr{
public function __construct($name, $value) {
echo "$name,$value";
}
}
#[MyAttr("api","http://www.yzmedu.com/api")]
class Person{}
$ref=new ReflectionClass("Person");
$ref->getAttributes("MyAttr")[0]->newInstance();
```
 
**十一.類注解高級配置**
```
#[Attribute(Attribute::TARGET_CLASS)]
class Myattr{
public $name;
public $value;
public $number;
public function __construct($name,$value,$number){
$this->name=$name;
$this->value=$value;
$this->number=$number;
}
public function say(){
echo "{$this->name}--{$this->value}--{$this->number}";
}
}
#[Myattr('api','http://www.yzmedu.com/api','10')]
class Person{}
$ref=new ReflectionClass("Person");
$attr=$ref->getAttributes('Myattr')[0];
$obj=$attr->newInstance();
$obj->say();
```
 
### **系統的學習PHP**
關注:PHP自學中心,回復相應的關鍵詞,領取以下視頻教程
**Redis6.0全套實戰教程【面試題講解】**
公眾號里回復:884613
 
#### **還有其他的教程的關鍵詞,請關注公眾號查看每天分享的文章教程的頭部**

- 第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的別名函數棄用