mirror of
https://github.com/php/php-src.git
synced 2026-04-19 14:01:01 +02:00
Make ReflectionAttribute::newInstance() respect the strict_types=1 declaration at the attribute use-site. More generally, pretend that we are calling the attribute constructor from the place where the attribute is used, which also means that the attribute location will show up properly in backtraces and inside "called in" error information. This requires us to store the attributes strict_types scope (as flags), as well as the attribute line number. The attribute filename can be recovered from the symbol it is used on. We might want to expose the attribute line number via reflection as well. See also https://externals.io/message/111915. Closes GH-6201.
98 lines
1.7 KiB
PHP
98 lines
1.7 KiB
PHP
--TEST--
|
|
Backtrace during attribute instance creation
|
|
--FILE--
|
|
<?php
|
|
|
|
#[Attribute]
|
|
class MyAttribute {
|
|
public function __construct() {
|
|
debug_print_backtrace();
|
|
var_dump(debug_backtrace());
|
|
var_dump((new Exception)->getTrace());
|
|
}
|
|
}
|
|
|
|
#[MyAttribute]
|
|
class Test {}
|
|
|
|
(new ReflectionClass(Test::class))->getAttributes()[0]->newInstance();
|
|
|
|
?>
|
|
--EXPECTF--
|
|
#0 MyAttribute->__construct() called at [%s031_backtrace.php:12]
|
|
#1 ReflectionAttribute->newInstance() called at [%s:%d]
|
|
array(2) {
|
|
[0]=>
|
|
array(7) {
|
|
["file"]=>
|
|
string(%d) "%s031_backtrace.php"
|
|
["line"]=>
|
|
int(12)
|
|
["function"]=>
|
|
string(11) "__construct"
|
|
["class"]=>
|
|
string(11) "MyAttribute"
|
|
["object"]=>
|
|
object(MyAttribute)#1 (0) {
|
|
}
|
|
["type"]=>
|
|
string(2) "->"
|
|
["args"]=>
|
|
array(0) {
|
|
}
|
|
}
|
|
[1]=>
|
|
array(7) {
|
|
["file"]=>
|
|
string(%d) "%s"
|
|
["line"]=>
|
|
int(%d)
|
|
["function"]=>
|
|
string(11) "newInstance"
|
|
["class"]=>
|
|
string(19) "ReflectionAttribute"
|
|
["object"]=>
|
|
object(ReflectionAttribute)#2 (0) {
|
|
}
|
|
["type"]=>
|
|
string(2) "->"
|
|
["args"]=>
|
|
array(0) {
|
|
}
|
|
}
|
|
}
|
|
array(2) {
|
|
[0]=>
|
|
array(6) {
|
|
["file"]=>
|
|
string(%d) "%s031_backtrace.php"
|
|
["line"]=>
|
|
int(12)
|
|
["function"]=>
|
|
string(11) "__construct"
|
|
["class"]=>
|
|
string(11) "MyAttribute"
|
|
["type"]=>
|
|
string(2) "->"
|
|
["args"]=>
|
|
array(0) {
|
|
}
|
|
}
|
|
[1]=>
|
|
array(6) {
|
|
["file"]=>
|
|
string(%d) "%s"
|
|
["line"]=>
|
|
int(%d)
|
|
["function"]=>
|
|
string(11) "newInstance"
|
|
["class"]=>
|
|
string(19) "ReflectionAttribute"
|
|
["type"]=>
|
|
string(2) "->"
|
|
["args"]=>
|
|
array(0) {
|
|
}
|
|
}
|
|
}
|