Özniteliklerin Yansıtma Arayüzü ile Okunması

Sınıflar, yöntemler, işlevler, değiştirgeler, özellikler ve sınıf sabitlerinden özniteliklere erişmek için kullanılan yansıtma arayüzü, ilgili Reflection nesnelerinin her birine getAttributes() yöntemini sağlar. Bu yöntem, öznitelik adı, değiştirgeler ve temsil edilen özniteliğin bir örneğini örneklendirmek için sorgulanabilen ReflectionAttribute örneklerini içeren bir dizi döndürür.

Yansıtılan öznitelik temsilinin gerçek örnekten bu şekilde ayrılması, programcının eksik öznitelik sınıfları, yanlış yazılmış veya eksik değiştirgelerle ilgili hataları elde etme konusundaki denetimini artırır. Öznitelik sınıfının nesneleri, yalnızca ReflectionAttribute::newInstance() çağrıldıktan sonra başlatılırsa değiştirgelerin doğru eşleşmesi sağlanır, daha önce değil.

Örnek 1 - Özniteliklerin Yansıtma Arayüzü kullanılarak Okunması

<?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)
}
*/

Yansıtma örneğindeki tüm öznitelikleri yinelemek yerine, yalnızca belirli bir öznitelik sınıfı, sınıfın ismi değiştirge olarak aktarılarak okunabilir.

Örnek 2 - Yansıtma Arayüzünü kullanarak istenen özniteliklerin okunması

<?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());
    }
}

dumpAttributeData(new ReflectionClass(Thing::class));
add a note add a note

User Contributed Notes 1 note

up
2
Hirusha Sharma
3 years ago
Fetch properties from functions:

----------------------------------------
Function definition with attributes:
----------------------------------------
#[ReadOnly]
#[Property(type: 'function', name: 'Hello')]
function Hello()
{
    return "Hello";
}

-----------------------------------------
Gather attributes from the function
-----------------------------------------
function getAttributes(Reflector $reflection)
{
    $attributes = $reflection->getAttributes();
    $result = [];
    foreach ($attributes as $attribute)
    {
        $result[$attribute->getName()] = $attribute->getArguments();
    }
    return $result;
}

$reflection = new ReflectionFunction("Hello");
print_r(getAttributes($reflection));

-----------------------------
OUTPUT
-----------------------------
Array
(
    [ReadOnly] => Array
        (
        )

    [Property] => Array
        (
            [type] => function
            [name] => Hello
        )

)
To Top