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 is an immutable object has already create.'); } $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 Minny'; } } $four = new TryToBreakImmutableObject('Pippo'); echo $four->getValue(); //Pippo echo $four->change(); echo $four->getValue();
Output for 7.2.0 - 7.2.28, 7.3.0 - 7.3.15, 7.4.0 - 7.4.3
Fatal error: Class TryToBreakImmutableObject may not inherit from final class (SomeImmutableObject) in /in/OsOoW on line 43
Process exited with code 255.

preferences:
185.25 ms | 1395 KiB | 56 Q