3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Node { private $label = '_'; private $links; private $attrs; public function __construct($label = '_') { $this -> setLabel($label); } public function getLabel() { return $this -> label; } public function setLabel($label) { if (!is_scalar($label)) { throw new ErrorException("Error Processing Request", 1); } $this -> label = $label; return $this -> getLabel(); } public function __get($name) { if (isset($this -> links -> $name)) { return $this -> links -> $name; } elseif (isset($this -> attrs -> $name)) { return $this -> attrs -> $name; } else { throw new ErrorException("Error Processing Request", 1); } } public function __set($name, $value) { if ($name == '_' && !($value instanceof Node || $value instanceof Link || $value instanceof Collection)) { throw new ErrorException("Error Processing Request", 1); } $name_type = ( (1 * (isset($this -> links -> $name) && count($this -> links -> $name) == 1)) | (2 * (isset($this -> links -> $name) && count($this -> links -> $name) <> 1)) | (3 * isset($this -> attrs -> $name)) | (13 * !(isset($this -> attrs -> $name) || isset($this -> links -> $name))) ); $value_type = ( (5 * ($value instanceof Collection)) | (7 * ($value instanceof Node)) | (17 * ($value instanceof Link)) | (11 * !($value instanceof Node || $value instanceof Link || $value instanceof Collection)) ); switch ($name_type * $value_type) { case 5: case 10: { foreach ($value as $node) { $this -> links -> {$name} -> attach($node); } break; } case 7: case 14: { $this -> links -> {$name} -> attach($value); break; } case 11: case 22: { trigger_error('Converting ' . $name . ' from ' . ((count($this -> links -> $name) <> 1) ? 'Collection' : 'Node') . ' to property', E_USER_WARNING); unset($this -> links -> $name); $this -> attrs -> $name = $value; break; } case 15: { trigger_error('Converting ' . $name . ' from property to Collection', E_USER_WARNING); unset($this -> attrs -> $name); $this -> links -> $name = $value; break; } case 21: { trigger_error('Converting ' . $name . ' from property to Node', E_USER_WARNING); unset($this -> attrs -> $name); $this -> links -> $name = new Collection; $this -> links -> {$name} -> attach($value); break; } case 33: case 143: { $this -> attrs -> $name = $value; break; } case 65: { $this -> links -> $name = $value; break; } case 91: { $this -> links -> $name = new Collection; $this -> links -> {$name} -> attach($value); break; } case 17: case 34: { $this -> links -> {$name} -> attach($value -> getTail()); $this -> links -> {$name} -> attach($value -> getHead()); break; } case 51: { trigger_error('Converting ' . $name . ' from property to Collection', E_USER_WARNING); unset($this -> attrs -> $name); $this -> links -> $name = new Collection; $this -> links -> {$name} -> attach($value -> getTail()); $this -> links -> {$name} -> attach($value -> getHead()); break; } case 221: { $this -> links -> $name = new Collection; $this -> links -> {$name} -> attach($value -> getTail()); $this -> links -> {$name} -> attach($value -> getHead()); break; } default: { throw new ErrorException("Error Processing Request", 1); // no break } } } public function __isset($name) { return (isset($this -> links -> $name) || isset($this -> attrs -> $name)); } public function __unset($name) { unset($this -> links -> $name, $this -> attrs -> $name); } public function serialize() { return serialize(array( 'label' => $this -> getLabel(), 'links' => $this -> links, 'attrs' => $this -> attrs )); } public function unserialize($serialized) { $unserialized = unserialize($serialized); $this -> setLabel($unserialized['label']); $this -> links = $unserialized['links']; $this -> attrs = $unserialized['attrs']; } } $node = new Node; $node->self = $node; echo serialize($node);

preferences:
36.28 ms | 402 KiB | 5 Q