note 127163 deleted from language.attributes.overview by crell
| From: | crell@php.net | Date: | Thu, 09 Jun 2022 16:10:17 +0000 |
| Subject: | note 127163 deleted from language.attributes.overview by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218576@lists.php.net to get a copy of this message | ||
Note Submitter: ScorpioT1000 at ya dot ru
----
Example attribute for parameters usage:
<?php
#[Attribute]
class MyAttribute
{
public function __construct(
protected string $data = 'default data'
) {
}
}
function myFunction(
#[MyAttribute(data: 'custom data')]
string $myParameter
) {
}
$rAttribute = (new ReflectionFunction('myFunction'))
->getParameters()[0]
->getAttributes()[0];
var_dump($rAttribute);
$attributeInstance = $rAttribute->newInstance();
var_dump($attributeInstance);
?>
Result:
object(ReflectionAttribute)#1 (0) {
}
object(MyAttribute)#2 (1) {
["data":protected]=>
string(11) "custom data"
}