3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Foo implements JsonSerializable { private $foo = 1; private $serializer; public function setSerializer(Closure $serializer) { $this->serializer = $serializer; } public function jsonSerialize() { if (method_exists($this->serializer, 'call')) { return $this->serializer->call($this); } return call_user_func($this->serializer->bindTo($this, $this)); } } class Serializer { public function formatThisWay() { return function() { return $this->foo * 10; }; } public function formatThatWay() { return function() { return ['foo' => $this->foo * 10]; }; } } $foo = new Foo; $serializer = new Serializer; $foo->setSerializer($serializer->formatThisWay()); echo json_encode($foo); $foo->setSerializer($serializer->formatThatWay()); echo json_encode($foo);
Output for rfc.property-hooks, git.master, git.master_jit
Deprecated: Return type of Foo::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/DBFJT on line 13 10{"foo":10}

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:
81.27 ms | 2369 KiB | 4 Q