Skip to content

Commit bf87586

Browse files
committed
Allow extending readonly classes by non-readonly ones
1 parent 865f2c0 commit bf87586

8 files changed

+96
-21
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
--TEST--
2-
Non-readonly class cannot extend a readonly class
2+
Readonly class cannot extend a non-readonly class
33
--FILE--
44
<?php
55

6-
readonly class Foo
6+
class Foo
77
{
88
}
99

10-
class Bar extends Foo
10+
readonly class Bar extends Foo
1111
{
1212
}
1313

1414
?>
1515
--EXPECTF--
16-
Fatal error: Non-readonly class Bar cannot extend readonly class Foo in %s on line %d
16+
Fatal error: Readonly class Bar cannot extend non-readonly class Foo in %s on line %d

Zend/tests/readonly_classes/readonly_class_inheritance_error2.phpt

Lines changed: 0 additions & 16 deletions
This file was deleted.

Zend/tests/readonly_classes/readonly_class_inheritance_success.phpt renamed to Zend/tests/readonly_classes/readonly_class_inheritance_success1.phpt

File renamed without changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Non-readonly class can extend a readonly class
3+
--FILE--
4+
<?php
5+
6+
readonly class Foo
7+
{
8+
}
9+
10+
class Bar extends Foo
11+
{
12+
}
13+
14+
?>
15+
--EXPECT--
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Non-readonly child of a readonly class can create dynamic properties
3+
--FILE--
4+
<?php
5+
6+
readonly class Foo
7+
{
8+
}
9+
10+
class Bar extends Foo
11+
{
12+
}
13+
14+
$bar = new Bar();
15+
$bar->baz = 1;
16+
17+
?>
18+
--EXPECTF--
19+
Deprecated: Creation of dynamic property Bar::$baz is deprecated in %s on line %d
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Non-readonly child of a readonly class can accept the AllowDynamicProperties attribute
3+
--FILE--
4+
<?php
5+
6+
readonly class Foo
7+
{
8+
}
9+
10+
11+
#[AllowDynamicProperties]
12+
class Bar extends Foo
13+
{
14+
}
15+
16+
$bar = new Bar();
17+
$bar->baz = 1;
18+
19+
?>
20+
--EXPECTF--
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
Non-readonly child of a readonly class can declare non-readonly properties
3+
--FILE--
4+
<?php
5+
6+
readonly class Foo
7+
{
8+
}
9+
10+
class Bar extends Foo
11+
{
12+
public static $property1 = 1;
13+
public static int $property2 = 2;
14+
public $property3 = 3;
15+
public int $property4 = 4;
16+
}
17+
18+
$bar = new Bar();
19+
Bar::$property1 = 2;
20+
Bar::$property2 = 3;
21+
$bar->property3 = 4;
22+
$bar->property4 = 5;
23+
24+
var_dump(Bar::$property1);
25+
var_dump(Bar::$property2);
26+
var_dump($bar);
27+
28+
?>
29+
--EXPECT--
30+
int(2)
31+
int(3)
32+
object(Bar)#1 (2) {
33+
["property3"]=>
34+
int(4)
35+
["property4"]=>
36+
int(5)
37+
}

Zend/zend_inheritance.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1461,7 +1461,7 @@ ZEND_API void zend_do_inheritance_ex(zend_class_entry *ce, zend_class_entry *par
14611461
}
14621462
}
14631463

1464-
if (UNEXPECTED((ce->ce_flags & ZEND_ACC_READONLY_CLASS) != (parent_ce->ce_flags & ZEND_ACC_READONLY_CLASS))) {
1464+
if (UNEXPECTED((ce->ce_flags & ZEND_ACC_READONLY_CLASS) && !(parent_ce->ce_flags & ZEND_ACC_READONLY_CLASS))) {
14651465
zend_error_noreturn(E_COMPILE_ERROR, "%s class %s cannot extend %s class %s",
14661466
ce->ce_flags & ZEND_ACC_READONLY_CLASS ? "Readonly" : "Non-readonly", ZSTR_VAL(ce->name),
14671467
parent_ce->ce_flags & ZEND_ACC_READONLY_CLASS ? "readonly" : "non-readonly", ZSTR_VAL(parent_ce->name)

0 commit comments

Comments
 (0)