3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Main implements \JsonSerializable { private $objA=[], $objB=[]; public function start() { for ($i = 0; $i < 10; $i++) { $objB=$this->getObjBandAddToStack($i); $this->objA[]=new ClassA($objB); } } private function getObjBandAddToStack($i) { if(isset($this->objB[$i/2])) { $objB=$this->objB[$i/2]; } else { $objB=new ClassB(); $this->objB[$i]=$objB; } return $objB; } public function jsonSerialize(){ return [ 'objA' => $this->objA, 'objB' => $this->objB, ]; } } class ClassA implements \JsonSerializable { private $objB; public function __construct(ClassB $objB) { $this->objB=$objB; $objB->joinGroup($this); } public function jsonSerialize(){ return [ 'objB' => $this->objB, ]; } } class ClassB implements \JsonSerializable { private $objAgroup=[]; public function joinGroup($objA) { $this->objAgroup[]=$objA; } public function jsonSerialize(){ return [ 'objAgroup' => $this->objAgroup, ]; } } $main=new Main(); $main->start(); var_dump(json_encode($main)); var_dump(json_last_error()); var_dump(json_last_error_msg());
Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.4.16, 8.5.0 - 8.5.1
Deprecated: Return type of Main::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/B6LVu on line 25 Deprecated: Return type of ClassA::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/B6LVu on line 42 Deprecated: Return type of ClassB::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/B6LVu on line 57 Deprecated: Implicit conversion from float 0.5 to int loses precision in /in/B6LVu on line 15 Deprecated: Implicit conversion from float 0.5 to int loses precision in /in/B6LVu on line 16 Deprecated: Implicit conversion from float 1.5 to int loses precision in /in/B6LVu on line 15 Deprecated: Implicit conversion from float 2.5 to int loses precision in /in/B6LVu on line 15 Deprecated: Implicit conversion from float 2.5 to int loses precision in /in/B6LVu on line 16 Deprecated: Implicit conversion from float 3.5 to int loses precision in /in/B6LVu on line 15 Deprecated: Implicit conversion from float 3.5 to int loses precision in /in/B6LVu on line 16 Deprecated: Implicit conversion from float 4.5 to int loses precision in /in/B6LVu on line 15 bool(false) int(6) string(18) "Recursion detected"
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.
Output for 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
bool(false) int(6) string(18) "Recursion detected"

preferences:
190.54 ms | 411 KiB | 5 Q