3v4l.org

run code in 300+ PHP versions simultaneously
<?php 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 AnotherClassToBreakImmutableObject extends SomeImmutableObject { 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'; } } $three = new AnotherClassToBreakImmutableObject('Foo'); echo $three->getValue(); //Foo echo $three->change(); echo $three->getValue(); //the value is: Foo and Baz -
Output for 8.2.0 - 8.2.25, 8.3.0 - 8.3.14
Deprecated: Creation of dynamic property AnotherClassToBreakImmutableObject::$someString is deprecated in /in/JmkoK on line 29 the value is:Foo - the value is:Foo and Baz -
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.30
the value is:Foo - the value is:Foo and Baz -

preferences:
57.66 ms | 408 KiB | 5 Q