Commit 49e62e2b962 for php
commit 49e62e2b9622181a10838a676a67871a43e8febe
Author: Ilija Tovilo <ilija.tovilo@me.com>
Date: Mon Nov 2 18:13:52 2026 +0100
Fix propagation of public(set) of readonly trait property
Closes GH-25161
diff --git a/NEWS b/NEWS
index 3631b3bc908..60a6b22ed7b 100644
--- a/NEWS
+++ b/NEWS
@@ -33,6 +33,7 @@ PHP NEWS
(Ostap Brehin)
. Fixed stack limit fallback on FreeBSD threads other than the main one.
(Ostap Brehin)
+ . Fixed propagation of public(set) of readonly trait property. (ilutov)
- Curl:
. Fixed curl_reset() retaining CURLOPT_PRIVATE after reset. (lacatoire)
diff --git a/Zend/tests/asymmetric_visibility/trait_readonly_public_set.phpt b/Zend/tests/asymmetric_visibility/trait_readonly_public_set.phpt
new file mode 100644
index 00000000000..570524abfa1
--- /dev/null
+++ b/Zend/tests/asymmetric_visibility/trait_readonly_public_set.phpt
@@ -0,0 +1,46 @@
+--TEST--
+public(set) of trait property is properly propagated
+--FILE--
+<?php
+
+trait T {
+ public public(set) readonly int $pub;
+ protected readonly int $prot;
+ private readonly int $priv;
+
+ public function modify($prop) {
+ $this->{$prop} = 42;
+ try {
+ $this->{$prop} = 43;
+ } catch (\Throwable $e) {
+ echo $e::class, ': ', $e->getMessage(), "\n";
+ }
+ var_dump($this->{$prop});
+ }
+}
+
+class C {
+ use T;
+}
+
+$c = new C;
+
+$c->pub = 42;
+try {
+ $c->pub = 43;
+} catch (\Throwable $e) {
+ echo $e::class, ': ', $e->getMessage(), "\n";
+}
+var_dump($c->pub);
+
+$c->modify('prot');
+$c->modify('priv');
+
+?>
+--EXPECT--
+Error: Cannot modify readonly property C::$pub
+int(42)
+Error: Cannot modify readonly property C::$prot
+int(42)
+Error: Cannot modify readonly property C::$priv
+int(42)
diff --git a/Zend/zend_inheritance.c b/Zend/zend_inheritance.c
index 4d73dd003a3..6d702b67a22 100644
--- a/Zend/zend_inheritance.c
+++ b/Zend/zend_inheritance.c
@@ -2898,6 +2898,11 @@ static void zend_do_traits_property_binding(zend_class_entry *ce, zend_class_ent
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&traits[i]->properties_info, prop_name, property_info) {
uint32_t flags = property_info->flags;
+ /* ZEND_ACC_PUBLIC_SET is removed at compile-time. */
+ if ((flags & (ZEND_ACC_READONLY|ZEND_ACC_PPP_MASK|ZEND_ACC_PPP_SET_MASK)) == (ZEND_ACC_READONLY|ZEND_ACC_PUBLIC)) {
+ flags |= ZEND_ACC_PUBLIC_SET;
+ }
+
/* next: check for conflicts with current class */
if ((colliding_prop = zend_hash_find_ptr(&ce->properties_info, prop_name)) != NULL) {
if ((colliding_prop->flags & ZEND_ACC_PRIVATE) && colliding_prop->ce != ce) {