3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php abstract class Base{ protected $funcs = []; public function __call($name, $args) { echo("\t>>>Call [$name](". json_encode($args). ") \r\n"); if (method_exists($this, $name)) { return call_user_func_array($this->$name, $args); } else if (isset($this->funcs[ $name ])) { return call_user_func_array($this->funcs[ $name ], $args); } else { throw new Exception("Method <$name> is not accessible."); } } /** * @param array $args * * @example new Base(["field1"=>"value1", "field2"=>"value2"]) * if u need override __constructor, then use: * __construct($args){ * .. * parent::__construct($args); * } */ public function __construct($args = []) { $nameClass = get_class($this); #region auto create methods 'hasField' for everyone field\property (if there exists getter or setter) $properties = get_class_vars($nameClass); $keys = array_keys($properties); foreach ($keys AS $k) { if ((method_exists($this, "set" . ucfirst($k)) || method_exists($this, "get" . ucfirst($k))) && (!method_exists($this, "has" . ucfirst($k) && !isset($this->funcs["has" . ucfirst($k)]))) ) { $v = &$this->$k; $this->funcs["has" . ucfirst($k)] = function () use (&$v) { return !empty($v); }; } } #endregion foreach ($args as $k => $v) { if (property_exists($nameClass, $k)) { $this->$k = $v; } } } } class Test extends Base{ protected $field1; public function setField1($v){$this->field1 = $v;} public function getField1(){return $this->field1;} } $test = new Test(); $test->setField1(23423); echo $test->hasField1();
based on COHN4
Output for 5.4.0 - 7.2.0
>>>Call [hasField1]([]) 1
Output for 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/kH7hD on line 3
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/kH7hD on line 3
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_CLASS in /in/kH7hD on line 2
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_CLASS in /in/kH7hD on line 2
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/kH7hD on line 2
Process exited with code 255.