3v4l.org

run code in 300+ PHP versions simultaneously
<?php final class SomeImmutableObject { private $someString; private $flagCreate = false; public function __construct(string $value) { if ($this->flagCreate === true) { throw new \BadMethodCallException('This immutable object has already been created.'); } $this->someString = $value; $this->flagCreate = true; } public function getValue(): string { return 'the value is:' . $this->someString . ' - '; } } class TryToBreakImmutableObject extends SomeImmutableObject { public $someString; public function __construct(string $value) { $this->someString = $value; } public function getValue(): string { return 'the value is:' . $this->someString . ' - '; } public function change(): void { $this->someString .= ' and Baz'; } } $four = new TryToBreakImmutableObject('Foo'); echo $four->getValue(); //Foo echo $four->change(); echo $four->getValue();
Output for 7.2.0 - 7.2.32, 7.3.0 - 7.3.20, 7.4.0 - 7.4.8
Fatal error: Class TryToBreakImmutableObject may not inherit from final class (SomeImmutableObject) in /in/3vjQ0 on line 43
Process exited with code 255.

preferences:
156.56 ms | 1443 KiB | 7 Q