002_rfcexample.phpt 928 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. --TEST--
  2. Attributes: Example from Attributes RFC
  3. --FILE--
  4. <?php
  5. // https://wiki.php.net/rfc/attributes_v2#attribute_syntax
  6. namespace My\Attributes {
  7. use Attribute;
  8. #[Attribute]
  9. class SingleArgument {
  10. public $argumentValue;
  11. public function __construct($argumentValue) {
  12. $this->argumentValue = $argumentValue;
  13. }
  14. }
  15. }
  16. namespace {
  17. use My\Attributes\SingleArgument;
  18. #[SingleArgument("Hello World")]
  19. class Foo {
  20. }
  21. $reflectionClass = new \ReflectionClass(Foo::class);
  22. $attributes = $reflectionClass->getAttributes();
  23. var_dump($attributes[0]->getName());
  24. var_dump($attributes[0]->getArguments());
  25. var_dump($attributes[0]->newInstance());
  26. }
  27. ?>
  28. --EXPECTF--
  29. string(28) "My\Attributes\SingleArgument"
  30. array(1) {
  31. [0]=>
  32. string(11) "Hello World"
  33. }
  34. object(My\Attributes\SingleArgument)#%d (1) {
  35. ["argumentValue"]=>
  36. string(11) "Hello World"
  37. }