1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
--TEST--
Named params in attributes
--FILE--
<?php
#[Attribute]
class MyAttribute {
public function __construct(
public $a = 'a',
public $b = 'b',
public $c = 'c',
) {}
}
#[MyAttribute('A', c: 'C')]
class Test1 {}
#[MyAttribute('A', a: 'C')]
class Test2 {}
$attr = (new ReflectionClass(Test1::class))->getAttributes()[0];
var_dump($attr->getName());
var_dump($attr->getArguments());
var_dump($attr->newInstance());
$attr = (new ReflectionClass(Test2::class))->getAttributes()[0];
try {
var_dump($attr->newInstance());
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
?>
--EXPECT--
string(11) "MyAttribute"
array(2) {
[0]=>
string(1) "A"
["c"]=>
string(1) "C"
}
object(MyAttribute)#1 (3) {
["a"]=>
string(1) "A"
["b"]=>
string(1) "b"
["c"]=>
string(1) "C"
}
Named parameter $a overwrites previous argument
|