PHP 8 में, हम क्लासेस, प्रॉपर्टीज, और क्लास कॉन्स्टेंट्स, मेथड्स, फ़ंक्शन्स, पैरामीटर्स का उपयोग करके एट्रिब्यूट्स को एक्सेस करते हैं।
PHP 8 में, रिफ्लेक्शन API हर मैचिंग रिफ्लेक्शन ऑब्जेक्ट पर getAttribute() मेथड प्रदान करता है।
getAttribute() मेथड ReflectionAttribute चित्रणों की एक सरणी लौटाता है जिनसे एट्रिब्यूट नाम, आर्गुमेंट्स पूछा जा सकता है और दर्शाए गए एट्रिब्यूट का एक उदाहरण बनाया जा सकता है।
उदाहरण - PHP 8 में रिफ्लेक्शन API के साथ एट्रिब्यूट्स पढ़ना
<?php
#[Reading]
#[Property(type: 'function', name: 'Student')]
function Student()
{
return "Student";
}
function getAttributes(Reflector $reflection)
{
$attributes = $reflection->getAttributes();
$finalresult = [];
foreach ($attributes as $attribute)
{
$finalresult[$attribute->getName() ] = $attribute->getArguments();
}
return $finalresult;
}
$reflection = new ReflectionFunction("Student");
print_r(getAttributes($reflection));
?>
आउटपुट
Array (
[Reading] => Array
(
)
[Property] => Array
(
[type] => function
[name] => Student
)
)