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($other); } 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 git.master, git.master_jit, rfc.property-hooks
bool(true) bool(true) bool(false) bool(false)

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
48.84 ms | 401 KiB | 8 Q