3v4l.org

run code in 300+ PHP versions simultaneously
<?php const KEY_CLASS_NAME = "JSON\\NameSpaceThatAvoidsCollisionsWithFields\\className"; function arrayze($item){ if($item === NULL || is_scalar($item)){ return $item; }elseif(is_object($item)){ $className = get_class($item); $item = (Array)$item; $item[KEY_CLASS_NAME] = $className; } $array = []; foreach($item as $key => $content){ $array[base64_encode($key)] = arrayze($content); } return $array; } function dearrayze($item){ assert(is_object($item) === FALSE); if($item === NULL || is_scalar($item)){ return $item; } $itemClean = []; foreach($item as $key => $content){ $itemClean[base64_decode($key)] = dearrayze($content); } $item = $itemClean; $className = $item[KEY_CLASS_NAME] ?? NULL; if($className !== NULL){ $RC = new ReflectionClass($className); $object = $RC->newInstanceWithoutConstructor(); foreach($item as $property => $content){ if($property === KEY_CLASS_NAME) continue; // 0 ClassName 0 privateProperty // 0 * 0 protectedProperty // publicProperty if(count($p = explode("\0", $property)) === 3){ [$void, $scope, $actualPropertyName] = $p; $scope = $scope === "*" ? $className : $scope; (function() use($actualPropertyName, $content){ $this->{$actualPropertyName} = $content; })->bindTo($object, $scope)(); }else{ $object->{$property} = $content; } } $item = $object; } return $item; } function json_encode_object($object){ $arrayzed = arrayze($object); return json_encode($arrayzed, JSON_PRETTY_PRINT); } function json_decode_object(String $object){ $arrayzed = json_decode($object, TRUE); return dearrayze($arrayzed); } class Father{ public $fpublic = 1; protected $fprotected = 2; private $fprivate = 3; } class Child extends Father{ public $cpublic = 1; protected $cprotected = 2; private $cprivate = 3; public $whoops = "\xc2"; } $child = new Child(); var_dump($child); echo "encoded:\n"; $encoded = json_encode_object($child); var_dump($encoded); echo "decoded:\n"; $decoded = json_decode_object($encoded); var_dump($decoded);
Output for 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
object(Child)#1 (7) { ["fpublic"]=> int(1) ["fprotected":protected]=> int(2) ["fprivate":"Father":private]=> int(3) ["cpublic"]=> int(1) ["cprotected":protected]=> int(2) ["cprivate":"Child":private]=> int(3) ["whoops"]=> string(1) "�" } encoded: bool(false) decoded: NULL
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
object(Child)#1 (7) { ["cpublic"]=> int(1) ["cprotected":protected]=> int(2) ["cprivate":"Child":private]=> int(3) ["whoops"]=> string(1) "�" ["fpublic"]=> int(1) ["fprotected":protected]=> int(2) ["fprivate":"Father":private]=> int(3) } encoded: bool(false) decoded: NULL
Output for 7.0.0 - 7.0.33
Parse error: syntax error, unexpected '=' in /in/nFqDR on line 42
Process exited with code 255.
Output for 5.6.0 - 5.6.40
Parse error: syntax error, unexpected '?' in /in/nFqDR on line 32
Process exited with code 255.

preferences:
113.61 ms | 2030 KiB | 4 Q