男女疯狂一边摸一边做羞羞视频|啊好深好硬快点用力别停动态图|亚洲一区无码中文字幕|特级无码毛片免费视频播放▽|久久狠狠躁免费观看|国内精品久久久久久网站

使用反射 API 讀取注解

反射 API 提供了 getAttributes() 方法, 類(lèi)、方法、函數、參數、屬性、類(lèi)常量的反射對象可通過(guò)它獲取相應的注解。 該方法返回了 ReflectionAttribute 實(shí)例的數組, 可用于查詢(xún)注解名稱(chēng)、參數、也可以實(shí)例化一個(gè)注解。

實(shí)例和反射注解的分離使得程序員增加了在丟失反射類(lèi)、類(lèi)型錯誤、丟失參數等情況下的處理能力,也能處理錯誤。 只有調用 ReflectionAttribute::newInstance() 后,注解類(lèi)的對象才會(huì )以驗證過(guò)匹配的參數來(lái)實(shí)例化。

示例 #1 通過(guò)反射 API 讀取注解

<?php

#[Attribute]
class MyAttribute
{
    public 
$value;

    public function 
__construct($value)
    {
        
$this->value $value;
    }
}

#[MyAttribute(value: 1234)]
class Thing
{
}

function 
dumpAttributeData($reflection) {
    
$attributes $reflection->getAttributes();

    foreach (
$attributes as $attribute) {
       
var_dump($attribute->getName());
       
var_dump($attribute->getArguments());
       
var_dump($attribute->newInstance());
    }
}

dumpAttributeData(new ReflectionClass(Thing::class));
/*
string(11) "MyAttribute"
array(1) {
  ["value"]=>
  int(1234)
}
object(MyAttribute)#3 (1) {
  ["value"]=>
  int(1234)
}
*/

通過(guò)傳入參數:待搜索的注解類(lèi)名,可返回指定的注解類(lèi), 而不需要再到反射類(lèi)中迭代循環(huán)獲取所有注解。

示例 #2 使用反射 API 讀取指定的注解

<?php

function dumpMyAttributeData($reflection) {
    
$attributes $reflection->getAttributes(MyAttribute::class);

    foreach (
$attributes as $attribute) {
       
var_dump($attribute->getName());
       
var_dump($attribute->getArguments());
       
var_dump($attribute->newInstance());
    }
}

dumpMyAttributeData(new ReflectionClass(Thing::class));
男女疯狂一边摸一边做羞羞视频|啊好深好硬快点用力别停动态图|亚洲一区无码中文字幕|特级无码毛片免费视频播放▽|久久狠狠躁免费观看|国内精品久久久久久网站