xp-framework / compiler

Compiles future PHP to today's PHP.
19 stars 0 forks source link

Implement readonly modifier for classes #138

Closed thekid closed 2 years ago

thekid commented 2 years ago

See https://wiki.php.net/rfc/readonly_classes and xp-framework/ast#37. The following comes quite close to records, although it doesn't declare getters but uses public readonly fields:

readonly class Point {
  public function __construct(
    public int $x,
    public int $y,
  ) { }
}

$point= new Point(8, 42);
echo "Hello {$point->x}x{$point->y}!\n";

// Will not modify the member, raising an exception instead:
// $point->x= 9;
thekid commented 2 years ago

Released in https://github.com/xp-framework/compiler/releases/tag/v8.4.0 - removing the emulation from PHP 8.2 emitter can be done once the php-src PR is merged, and released in a separate release.