[php-src] PHP-8.6: Fix propagation of public(set) of readonly trait property

From: Date: Mon, 23 Nov 2026 14:49:56 +0000
Subject: [php-src] PHP-8.6: Fix propagation of public(set) of readonly trait property
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-144053@lists.php.net to get a copy of this message
Author: Ilija Tovilo (iluuu1994) Date: 2026-11-23T15:47:22+01:00 Commit: https://github.com/php/php-src/commit/49e62e2b9622181a10838a676a67871a43e8febe Raw diff: https://github.com/php/php-src/commit/49e62e2b9622181a10838a676a67871a43e8febe.diff Fix propagation of public(set) of readonly trait property Closes GH-25161 Changed paths: A Zend/tests/asymmetric_visibility/trait_readonly_public_set.phpt M NEWS M Zend/zend_inheritance.c Diff: diff --git a/NEWS b/NEWS index 3631b3bc9088..60a6b22ed7bb 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 000000000000..570524abfa1a --- /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 4d73dd003a31..6d702b67a22d 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) {

« previous php.cvs (#144053) next »