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();
based on OsOoW
Output for 7.2.0 - 7.2.32, 7.3.0 - 7.3.20, 7.4.0 - 7.4.8, 8.0.0alpha1 - alpha2
Fatal error: Class TryToBreakImmutableObject may not inherit from final class (SomeImmutableObject) in /in/3vjQ0 on line 43
Process exited with code 255.