Skip to content

Instantly share code, notes, and snippets.

@shin1x1
Last active September 5, 2020 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shin1x1/d6004d4bd5ca81192fb1adb48d91789f to your computer and use it in GitHub Desktop.
Save shin1x1/d6004d4bd5ca81192fb1adb48d91789f to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
@@Attribute
final class Attr1 {
public function __construct(private string $value) {}
}
final class Attr2 {
public function __construct() {}
}
@@Attr1("Hello World")
@@Attr2
@@NotFound
class Foo {
}
$reflectionClass = new \ReflectionClass(Foo::class);
$attributes = $reflectionClass->getAttributes();
foreach ($attributes as $attr) {
var_dump($attr->getName());
var_dump($attr->getArguments());
// newInstance() は @@Attribute が付いたクラスで無いと Fatal Error になる
// var_dump($attr->newInstance());
}
<?php
declare(strict_types=1);
<<PhpAttribute>>
final class Attr1 {
public function __construct(private string $value) {}
}
final class Attr2 {
public function __construct() {}
}
<<Attr1("Hello World")>>
<<Attr2>>
<<NotFound>>
class Foo {
}
$reflectionClass = new \ReflectionClass(Foo::class);
$attributes = $reflectionClass->getAttributes();
foreach ($attributes as $attr) {
var_dump($attr->getName());
var_dump($attr->getArguments());
// newInstance() は <<PhpAttribute>> が付いたクラスで無いと Fatal Error になる
// var_dump($attr->newInstance());
}
<?php
declare(strict_types=1);
#[Attribute]
final class Attr1 {
public function __construct(private string $value) {}
}
final class Attr2 {
public function __construct() {}
}
#[Attr1("Hello World")]
#[Attr2]
#[NotFound]
class Foo {
}
$reflectionClass = new \ReflectionClass(Foo::class);
$attributes = $reflectionClass->getAttributes();
foreach ($attributes as $attr) {
var_dump($attr->getName());
var_dump($attr->getArguments());
// newInstance() は #[Attribute] が付いたクラスで無いと Fatal Error になる
// var_dump($attr->newInstance());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment