Skip to content

Commit edaf89c

Browse files
committed
Emit readonly classes natively in PHP 8.3
1 parent 3191cbb commit edaf89c

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

ChangeLog.md

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ XP Compiler ChangeLog
33

44
## ?.?.? / ????-??-??
55

6+
* Changed emitter to use native readonly classes in PHP 8.3, fixing an
7+
inconsistency with accessing undefined properties
8+
(@thekid)
9+
610
## 9.2.0 / 2024-08-27
711

812
* Merged PR #183: Add emitting support for asymmetric visibility. See

src/main/php/lang/ast/emit/PHP83.class.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @see https://wiki.php.net/rfc#php_83
1919
*/
2020
class PHP83 extends PHP {
21-
use RewriteBlockLambdaExpressions, RewriteProperties, ReadonlyClasses;
21+
use RewriteBlockLambdaExpressions, RewriteProperties;
2222

2323
public $targetVersion= 80300;
2424

src/main/php/lang/ast/emit/ReadonlyClasses.class.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ protected function emitClass($result, $class) {
3333
}
3434

3535
// Prevent dynamic members
36-
$throw= new Code('throw new \\Error("Cannot create dynamic property ".__CLASS__."::".$name);');
3736
$context= $result->codegen->enter(new InType($class));
38-
$context->virtual[null]= [$throw, $throw];
37+
$context->virtual[null]= [
38+
new Code('trigger_error("Undefined property: ".__CLASS__."::\$".$name, E_USER_WARNING); return $_;'),
39+
new Code('throw new \\Error("Cannot create dynamic property ".__CLASS__."::".$name);')
40+
];
3941
}
4042

4143
return parent::emitClass($result, $class);

src/test/php/lang/ast/unittest/emit/ReadonlyTest.class.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,11 @@ public function cannot_have_an_initial_value() {
174174
}');
175175
}
176176

177-
#[Test, Expect(class: Error::class, message: '/Cannot create dynamic property .+fixture/')]
178-
public function cannot_read_dynamic_members_from_readonly_classes() {
177+
#[Test]
178+
public function reading_dynamic_members_from_readonly_classes_causes_warning() {
179179
$t= $this->declare('readonly class %T { }');
180-
$t->newInstance()->fixture;
180+
Assert::null($t->newInstance()->fixture);
181+
\xp::gc();
181182
}
182183

183184
#[Test, Expect(class: Error::class, message: '/Cannot create dynamic property .+fixture/')]

0 commit comments

Comments
 (0)