Last active
September 5, 2020 06:23
-
-
Save shin1x1/d6004d4bd5ca81192fb1adb48d91789f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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