Skip to content

Commit 34c00a9

Browse files
committed
__PROPERTY__ does not work in all constant expression contexts
Fixes GH-17222
1 parent 3cf381e commit 34c00a9

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

Zend/tests/gh17222.phpt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
9+
class Foo
10+
{
11+
#[MyAttr(msg: __PROPERTY__)]
12+
public string $i = __PROPERTY__ {
13+
#[MyAttr(msg: __PROPERTY__)]
14+
get {
15+
var_dump(__PROPERTY__);
16+
return $this->i;
17+
}
18+
set (#[MyAttr(msg: __PROPERTY__)] string $v) {
19+
$this->i = $v;
20+
}
21+
}
22+
}
23+
24+
$f = new Foo();
25+
var_dump($f->i);
26+
27+
$r = new ReflectionClass(Foo::class);
28+
$p = $r->getProperty('i');
29+
var_dump($p->getAttributes()[0]->getArguments()['msg']);
30+
31+
$get = $p->getHook(PropertyHookType::Get);
32+
var_dump($get->getAttributes()[0]->getArguments()['msg']);
33+
34+
$set = $p->getHook(PropertyHookType::Set);
35+
var_dump($set->getParameters()[0]->getAttributes()[0]->getArguments()['msg']);
36+
37+
?>
38+
--EXPECT--
39+
string(1) "i"
40+
string(1) "i"
41+
string(1) "i"
42+
string(1) "i"
43+
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)