note 128170 added to reflectionattribute.newinstance
| From: | baptiste at pillot dot fr | Date: | Mon, 30 Jan 2023 11:05:09 +0000 |
| Subject: | note 128170 added to reflectionattribute.newinstance | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220126@lists.php.net to get a copy of this message | ||
For ReflectionAttribute::newInstance to work without error, the attribute must be defined as a class
and have an #[Attribute] attribute.
Example :
<?php
#[Attribute]
class MyAttribute {}
#[MyAttribute]
class Test{}
$instance = (new ReflectionClass(Test::class))->getAttributes()[0]->newInstance();
var_dump($instance);
?>
This will output :
object(MyAttribute)#1 (0) {
}
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.158.213 (proxied: 37.167.132.212)
----
Manual Page -- https://php.net/manual/en/reflectionattribute.newinstance.php
Edit -- https://main.php.net/note/edit/128170
Del: integrated -- https://main.php.net/note/delete/128170/integrated
Del: useless -- https://main.php.net/note/delete/128170/useless
Del: bad code -- https://main.php.net/note/delete/128170/bad+code
Del: spam -- https://main.php.net/note/delete/128170/spam
Del: non-english -- https://main.php.net/note/delete/128170/non-english
Del: in docs -- https://main.php.net/note/delete/128170/in+docs
Del: other reasons-- https://main.php.net/note/delete/128170
Reject -- https://main.php.net/note/reject/128170
Search -- https://main.php.net/manage/user-notes.php