3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* * created on 1.2.2008 */ class RootObj implements JsonSerializable { private $_data; public function __construct($data = null) { $this->_data = $data; } public function &__get($name) { return $this->_data[$name]; } public function __set($name, $value) { $this->_data[$name] = $value; } public function __isset($name) { return isset($this->_data[$name]); } public static function createObjectsFromArray($className, $array, $defaults = [], $idAsKey = false) { $results = []; foreach ($array as $data) { $result = new $className ($data); if ($idAsKey) { if (is_object($data) && isset($data->id)) { $results[$data->id] = $result; } else { $results[$data['id']] = $result; } } else { $results[] = $result; } foreach ((array)$defaults as $key => $value) { $result->{$key} = $value; } } return $results; } public function asArray() { return $this->_data; } public static function collectionToIds($collection) { $ids = []; foreach ($collection as $item) { $id = null; if (is_array($item)) { $id = $item['id']; } else { $id = $item->id; } if ($id != null) { $ids[] = $id; } } return $ids; } public function getSaveSql($table) { $sqlSnippets = []; foreach ($this->_data as $key => $value) { if ($key == 'id') { continue; } if (is_array($value)) { continue; } if (is_object($value)) { continue; } if (is_bool($value)) { $value = $value ? 1 : 0; } if ($value === null) { $sqlSnippet = "{$key} = null"; } else { $sqlSnippet = "{$key} = '" . addslashes($value) . "'"; } $sqlSnippets[] = $sqlSnippet; } return ($this->_data['id'] > 0 ? 'update' : 'insert into') . " {$table} set " . implode(',', $sqlSnippets) . ($this->_data['id'] > 0 ? " where id = {$this->_data['id']}" : ''); } /** * @param string $propName * @param null|string $lang * * @return string */ public function _($propName, $lang = null) { if ($lang === null) { $lang = LangUtils::getLang(); } $localizedPropName = "{$propName}_{$lang}"; if ($lang === null || $lang === 'cs') { $localizedPropName = $propName; } return $this->{$localizedPropName}; } public function __toString() { $out = get_class($this) . ': '; $itemsTxt = []; foreach (array_merge($this->_data, get_object_vars_compatible($this)) as $key => $value) { $value = json_encode($value); $itemsTxt[] = "{$key} = {$value}"; } $out .= implode(',', $itemsTxt); return $out; } /** * Specify data which should be serialized to JSON * @link http://php.net/manual/en/jsonserializable.jsonserialize.php * @return mixed data which can be serialized by <b>json_encode</b>, * which is a value of any type other than a resource. * @since 5.4.0 */ public function jsonSerialize() { // we do not want to include classes like Maternia_Translate or ServiceManager $attr = get_object_vars_compatible($this); foreach ($attr as $k => $a) { if (is_object($a) && ! ($a instanceof stdClass)) { unset($attr[$k]); } } return add($this->_data, $attr); } public function __debugInfo() { $class = new \ReflectionClass($this); $properties = []; /** @var \ReflectionProperty $staticProperty */ foreach ($class->getStaticProperties() as $staticProperty) { var_dump($staticProperty); $staticProperty->setAccessible(true); $properties[$staticProperty->getName()] = $staticProperty->getValue(); } /** @var \ReflectionProperty $staticProperty */ foreach ($class->getProperties() as $property) { $property->setAccessible(true); $properties[$property->getName()] = $property->getValue($this); } var_dump($properties, $class->getParentClass()); $data = $properties['_data']; unset($properties['_data']); return array_merge($properties, $data); } } $a = new RootObj(); $a->sfdgsdfg = 'sfgsdfg'; class Bb extends RootObj { private $test = 'sdfs'; private static $sss = 'sdfs'; } $b = new Bb(); $b->sfsdfgsdfg = 'asf'; var_dump($a, $b);
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Deprecated: Return type of RootObj::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/r0dap on line 160 array(1) { ["_data"]=> array(1) { ["sfdgsdfg"]=> string(7) "sfgsdfg" } } bool(false) object(RootObj)#1 (1) { ["sfdgsdfg"]=> string(7) "sfgsdfg" } string(4) "sdfs" Warning: Uncaught Error: Call to a member function setAccessible() on string in /in/r0dap:181 Stack trace: #0 [internal function]: RootObj->__debugInfo() #1 /in/r0dap(209): var_dump(Object(RootObj), Object(Bb)) #2 {main} thrown in /in/r0dap on line 181 Fatal error: __debuginfo() must return an array in /in/r0dap on line 209
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Deprecated: Return type of RootObj::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/r0dap on line 160 array(1) { ["_data"]=> array(1) { ["sfdgsdfg"]=> string(7) "sfgsdfg" } } bool(false) object(RootObj)#1 (1) { ["sfdgsdfg"]=> string(7) "sfgsdfg" } string(4) "sdfs" Warning: Uncaught Error: Call to a member function setAccessible() on string in /in/r0dap:181 Stack trace: #0 [internal function]: RootObj->__debugInfo() #1 /in/r0dap(209): var_dump(Object(RootObj), Object(Bb)) #2 {main} thrown in /in/r0dap on line 181 Fatal error: __debuginfo() must return an array in /in/r0dap on line 209
Process exited with code 255.
Output for 7.1.8, 7.3.24 - 7.3.33, 7.4.12 - 7.4.33, 8.0.0 - 8.0.30
array(1) { ["_data"]=> array(1) { ["sfdgsdfg"]=> string(7) "sfgsdfg" } } bool(false) object(RootObj)#1 (1) { ["sfdgsdfg"]=> string(7) "sfgsdfg" } string(4) "sdfs" Warning: Uncaught Error: Call to a member function setAccessible() on string in /in/r0dap:181 Stack trace: #0 [internal function]: RootObj->__debugInfo() #1 /in/r0dap(209): var_dump(Object(RootObj), Object(Bb)) #2 {main} thrown in /in/r0dap on line 181 Fatal error: __debuginfo() must return an array in /in/r0dap on line 209
Process exited with code 255.

preferences:
145.48 ms | 402 KiB | 119 Q