Skip to content

Commit 147e9c8

Browse files
committed
__PROPERTY__ does not work in all constant expression contexts
Fixes GH-17222 Closes GH-17378
1 parent fd0cabb commit 147e9c8

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ PHP NEWS
99
with 0x0b). (nielsdos)
1010
. Fixed bug GH-16886 (ini_parse_quantity() fails to emit warning for 0x+0).
1111
(nielsdos)
12+
. Fixed bug GH-17222 (__PROPERTY__ magic constant does not work in all
13+
constant expression contexts). (ilutov)
1214

1315
- DOM:
1416
. Fixed bug GH-17397 (Assertion failure ext/dom/php_dom.c). (nielsdos)

Zend/tests/gh17222.phpt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
--TEST--
2+
GH-17222: __PROPERTY__ does not work in all constant expression contexts
3+
--FILE--
4+
<?php
5+
6+
#[Attribute]
7+
class MyAttribute {
8+
public function __construct(public string $msg) {}
9+
}
10+
11+
class Foo
12+
{
13+
#[MyAttr(msg: __PROPERTY__)]
14+
public string $i = __PROPERTY__ {
15+
#[MyAttr(msg: __PROPERTY__)]
16+
get {
17+
var_dump(__PROPERTY__);
18+
return $this->i;
19+
}
20+
set (#[MyAttr(msg: __PROPERTY__)] string $v) {
21+
$this->i = $v;
22+
}
23+
}
24+
}
25+
26+
$f = new Foo();
27+
var_dump($f->i);
28+
29+
$r = new ReflectionClass(Foo::class);
30+
$p = $r->getProperty('i');
31+
var_dump($p->getAttributes()[0]->getArguments()['msg']);
32+
33+
$get = $p->getHook(PropertyHookType::Get);
34+
var_dump($get->getAttributes()[0]->getArguments()['msg']);
35+
36+
$set = $p->getHook(PropertyHookType::Set);
37+
var_dump($set->getParameters()[0]->getAttributes()[0]->getArguments()['msg']);
38+
39+
?>
40+
--EXPECT--
41+
string(1) "i"
42+
string(1) "i"
43+
string(1) "i"
44+
string(1) "i"
45+
string(1) "i"

Zend/zend_compile.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8634,6 +8634,13 @@ static void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t f
86348634
zend_type type = ZEND_TYPE_INIT_NONE(0);
86358635
flags |= zend_property_is_virtual(ce, name, hooks_ast, flags) ? ZEND_ACC_VIRTUAL : 0;
86368636

8637+
/* FIXME: This is a dirty fix to maintain ABI compatibility. We don't
8638+
* have an actual property info yet, but we really only need the name
8639+
* anyway. We should convert this to a zend_string. */
8640+
ZEND_ASSERT(!CG(context).active_property_info);
8641+
zend_property_info dummy_prop_info = { .name = name };
8642+
CG(context).active_property_info = &dummy_prop_info;
8643+
86378644
if (!hooks_ast) {
86388645
if (ce->ce_flags & ZEND_ACC_INTERFACE) {
86398646
zend_error_noreturn(E_COMPILE_ERROR,
@@ -8726,6 +8733,8 @@ static void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t f
87268733
if (attr_ast) {
87278734
zend_compile_attributes(&info->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, 0);
87288735
}
8736+
8737+
CG(context).active_property_info = NULL;
87298738
}
87308739
}
87318740
/* }}} */

0 commit comments

Comments
 (0)