Skip to content

Commit a06668a

Browse files
committed
Merge branch 'PHP-8.4'
* PHP-8.4: __PROPERTY__ does not work in all constant expression contexts
2 parents af1e289 + 147e9c8 commit a06668a

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

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
@@ -8657,6 +8657,13 @@ static void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t f
86578657
zend_type type = ZEND_TYPE_INIT_NONE(0);
86588658
flags |= zend_property_is_virtual(ce, name, hooks_ast, flags) ? ZEND_ACC_VIRTUAL : 0;
86598659

8660+
/* FIXME: This is a dirty fix to maintain ABI compatibility. We don't
8661+
* have an actual property info yet, but we really only need the name
8662+
* anyway. We should convert this to a zend_string. */
8663+
ZEND_ASSERT(!CG(context).active_property_info);
8664+
zend_property_info dummy_prop_info = { .name = name };
8665+
CG(context).active_property_info = &dummy_prop_info;
8666+
86608667
if (!hooks_ast) {
86618668
if (ce->ce_flags & ZEND_ACC_INTERFACE) {
86628669
zend_error_noreturn(E_COMPILE_ERROR,
@@ -8749,6 +8756,8 @@ static void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t f
87498756
if (attr_ast) {
87508757
zend_compile_attributes(&info->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, 0);
87518758
}
8759+
8760+
CG(context).active_property_info = NULL;
87528761
}
87538762
}
87548763
/* }}} */

0 commit comments

Comments
 (0)