Skip to content

Commit

Permalink
Emit readonly classes natively in PHP 8.3
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Aug 27, 2024
1 parent 3191cbb commit 2c2c6d9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main/php/lang/ast/emit/PHP83.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @see https://wiki.php.net/rfc#php_83
*/
class PHP83 extends PHP {
use RewriteBlockLambdaExpressions, RewriteProperties, ReadonlyClasses;
use RewriteBlockLambdaExpressions, RewriteProperties;

public $targetVersion= 80300;

Expand Down
6 changes: 4 additions & 2 deletions src/main/php/lang/ast/emit/ReadonlyClasses.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ protected function emitClass($result, $class) {
}

// Prevent dynamic members
$throw= new Code('throw new \\Error("Cannot create dynamic property ".__CLASS__."::".$name);');
$context= $result->codegen->enter(new InType($class));
$context->virtual[null]= [$throw, $throw];
$context->virtual[null]= [
new Code(''),
new Code('throw new \\Error("Cannot create dynamic property ".__CLASS__."::".$name);')
];
}

return parent::emitClass($result, $class);
Expand Down
7 changes: 4 additions & 3 deletions src/test/php/lang/ast/unittest/emit/ReadonlyTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,11 @@ public function cannot_have_an_initial_value() {
}');
}

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

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

0 comments on commit 2c2c6d9

Please sign in to comment.