3v4l.org

run code in 300+ PHP versions simultaneously
<?php // note that copy() is a proper alternative to __clone() interface Any { function getHash(); function equals(); } class Mutable implements Any { protected $baz; function __construct(string $baz){ $this->setBaz($baz); } function setBaz(string $baz){ $this->baz = $baz; } function getBaz(){ return $this->baz; } function getHash(){ return Immutable::CLASS . "|" . spl_object_hash($this); } function equals($other){ return $this->getHash() === $other->getHash(); } function copy(){ return new static($this->baz); } } class Immutable implements Any { protected $baz; function __construct(string $baz){ $this->baz = $baz; } function getHash(){ return Mutable::CLASS . "|" . $this->baz; } function equals($other){ return $this->getHash() === $other->getHash(); } function copy(){ return $this; } } var_dump((new Immutable("baz"))->equals(new Immutable("baz"))); // true var_dump((new Immutable("bar"))->equals(new Immutable("bar"))); // true var_dump((new Mutable("baz"))->equals(new Mutable("baz"))); // false var_dump((new Mutable("bar"))->equals(new Mutable("bar"))); // false
Output for 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Fatal error: Declaration of Mutable::equals($other) must be compatible with Any::equals() in /in/2Redc on line 31
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.7, 7.1.20 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33
Fatal error: Declaration of Mutable::equals($other) must be compatible with Any::equals() in /in/2Redc on line 11
Process exited with code 255.
Output for 7.1.10

Process exited with code 137.
Output for 5.5.24 - 5.5.35, 5.6.7 - 5.6.28
Fatal error: Declaration of Mutable::equals() must be compatible with Any::equals() in /in/2Redc on line 12
Process exited with code 255.
Output for 5.4.2 - 5.4.45
Parse error: syntax error, unexpected 'CLASS' (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in /in/2Redc on line 28
Process exited with code 255.

preferences:
231 ms | 401 KiB | 272 Q