note 126222 added to reflectionclass.getattributes
| From: | sergiolibe at gmail dot com | Date: | Tue, 06 Jul 2021 15:11:18 +0000 |
| Subject: | note 126222 added to reflectionclass.getattributes | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-216582@lists.php.net to get a copy of this message | ||
When using getAttributes() with specific attribute class and flags, flag 0 will return just matching
attributes with specified class, and 2 will return matching attributes with specified class and
children of that class:
<?php
#[Attribute(\Attribute::TARGET_CLASS)]
class SomeAttribute {}
#[Attribute(\Attribute::TARGET_CLASS)]
class ChildAttribute extends SomeAttribute {}
#[SomeAttribute]
#[SomeChildAttribute]
class SomeClass {}
$rc = new ReflectionClass(SomeClass::class);
$r_atts = $rc->getAttributes(SomeAttribute::class, 0); // 0 is default, just given class
echo json_encode(array_map(fn(ReflectionAttribute $r_att) => $r_att->getName(), $r_atts)),
PHP_EOL;
$r_atts = $rc->getAttributes(SomeAttribute::class, 2); // given class and children classes
echo json_encode(array_map(fn(ReflectionAttribute $r_att) => $r_att->getName(), $r_atts)),
PHP_EOL;
?>
output:
["SomeAttribute"]
["SomeAttribute","ChildAttribute"]
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 217.9.121.88)
----
Manual Page -- https://php.net/manual/en/reflectionclass.getattributes.php
Edit -- https://main.php.net/note/edit/126222
Del: integrated -- https://main.php.net/note/delete/126222/integrated
Del: useless -- https://main.php.net/note/delete/126222/useless
Del: bad code -- https://main.php.net/note/delete/126222/bad+code
Del: spam -- https://main.php.net/note/delete/126222/spam
Del: non-english -- https://main.php.net/note/delete/126222/non-english
Del: in docs -- https://main.php.net/note/delete/126222/in+docs
Del: other reasons-- https://main.php.net/note/delete/126222
Reject -- https://main.php.net/note/reject/126222
Search -- https://main.php.net/manage/user-notes.php