Skip to content

Commit 2b8a1b4

Browse files
committed
Forbid a-vis on unilateral virtual prop
The get-only case is obvious, there is no set operation so specifying its visibility is senseless. The set-only case is also questionable, since there is no operation other than set, so changing the visibility of the entire property is preferable. Closes GH-15698
1 parent b81f972 commit 2b8a1b4

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ PHP NEWS
1212
. Implemented lazy objects RFC. (Arnaud)
1313
. Fixed bug GH-15686 (Building shared iconv with external iconv library).
1414
(Peter Kokot, zeriyoshi)
15+
. Fixed missing error when adding asymmetric visibility to unilateral virtual
16+
property. (ilutov)
1517

1618
- DOM:
1719
. Fixed bug GH-13988 (Storing DOMElement consume 4 times more memory in
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Get-only virtual property must not specify asymmetric visibility
3+
--FILE--
4+
<?php
5+
6+
class Foo {
7+
public private(set) int $bar {
8+
get => 42;
9+
}
10+
}
11+
12+
?>
13+
--EXPECTF--
14+
Fatal error: Unilateral virtual property Foo::$bar must not specify asymmetric visibility in %s on line %d
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Set-only virtual property must not specify asymmetric visibility
3+
--FILE--
4+
<?php
5+
6+
class Foo {
7+
public private(set) int $bar {
8+
set {}
9+
}
10+
}
11+
12+
?>
13+
--EXPECTF--
14+
Fatal error: Unilateral virtual property Foo::$bar must not specify asymmetric visibility in %s on line %d

Zend/zend_inheritance.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,6 +1706,12 @@ ZEND_API void zend_verify_hooked_property(zend_class_entry *ce, zend_property_in
17061706
zend_error_noreturn(E_COMPILE_ERROR,
17071707
"Abstract property %s::$%s must specify at least one abstract hook", ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
17081708
}
1709+
if ((prop_info->flags & ZEND_ACC_VIRTUAL)
1710+
&& (prop_info->flags & ZEND_ACC_PPP_SET_MASK)
1711+
&& (!prop_info->hooks[ZEND_PROPERTY_HOOK_GET] || !prop_info->hooks[ZEND_PROPERTY_HOOK_SET])) {
1712+
zend_error_noreturn(E_COMPILE_ERROR,
1713+
"Unilateral virtual property %s::$%s must not specify asymmetric visibility", ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
1714+
}
17091715
}
17101716

17111717
ZEND_API ZEND_COLD ZEND_NORETURN void zend_hooked_property_variance_error_ex(zend_string *value_param_name, zend_string *class_name, zend_string *prop_name)
@@ -1893,11 +1899,13 @@ ZEND_API void zend_do_inheritance_ex(zend_class_entry *ce, zend_class_entry *par
18931899
} ZEND_HASH_FOREACH_END();
18941900
}
18951901

1896-
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&ce->properties_info, key, property_info) {
1897-
if (property_info->ce == ce && property_info->hooks) {
1898-
zend_verify_hooked_property(ce, property_info, key);
1899-
}
1900-
} ZEND_HASH_FOREACH_END();
1902+
if (ce->num_hooked_props) {
1903+
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&ce->properties_info, key, property_info) {
1904+
if (property_info->ce == ce && property_info->hooks) {
1905+
zend_verify_hooked_property(ce, property_info, key);
1906+
}
1907+
} ZEND_HASH_FOREACH_END();
1908+
}
19011909

19021910
if (zend_hash_num_elements(&parent_ce->constants_table)) {
19031911
zend_class_constant *c;

0 commit comments

Comments
 (0)