3v4l.org

run code in 300+ PHP versions simultaneously
<?php abstract class Base{ protected $funcs = []; public function __call($name, $args) { error_log("\t>>>Call [$name](". json_encode($args). ") \r\n",3, LOGFILE); 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();
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Fatal error: Uncaught Error: Undefined constant "LOGFILE" in /in/COHN4:6 Stack trace: #0 /in/COHN4(60): Base->__call('hasField1', Array) #1 {main} thrown in /in/COHN4 on line 6
Process exited with code 255.
Output for 7.3.32 - 7.3.33, 7.4.33
Warning: Use of undefined constant LOGFILE - assumed 'LOGFILE' (this will throw an Error in a future version of PHP) in /in/COHN4 on line 6 Warning: error_log(LOGFILE): failed to open stream: Read-only file system in /in/COHN4 on line 6 1
Output for 7.2.29 - 7.2.33, 7.3.16 - 7.3.31, 7.4.0 - 7.4.32
Warning: Use of undefined constant LOGFILE - assumed 'LOGFILE' (this will throw an Error in a future version of PHP) in /in/COHN4 on line 6 Warning: error_log(): open_basedir restriction in effect. File(LOGFILE) is not within the allowed path(s): (/tmp:/in:/etc) in /in/COHN4 on line 6 Warning: error_log(LOGFILE): failed to open stream: Operation not permitted in /in/COHN4 on line 6 1
Output for 7.2.6
Warning: Use of undefined constant LOGFILE - assumed 'LOGFILE' (this will throw an Error in a future version of PHP) in /in/COHN4 on line 6 Warning: error_log(): open_basedir restriction in effect. File(LOGFILE) is not within the allowed path(s): (/tmp:/in) in /in/COHN4 on line 6 Warning: error_log(LOGFILE): failed to open stream: Operation not permitted in /in/COHN4 on line 6 1
Output for 7.2.0
Warning: Use of undefined constant LOGFILE - assumed 'LOGFILE' (this will throw an Error in a future version of PHP) in /in/COHN4 on line 6 Warning: error_log(LOGFILE): failed to open stream: Permission denied in /in/COHN4 on line 6 1
Output for 7.1.20
Notice: Use of undefined constant LOGFILE - assumed 'LOGFILE' in /in/COHN4 on line 6 Warning: error_log(): open_basedir restriction in effect. File(LOGFILE) is not within the allowed path(s): (/tmp:/in) in /in/COHN4 on line 6 Warning: error_log(LOGFILE): failed to open stream: Operation not permitted in /in/COHN4 on line 6 1
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.10
Notice: Use of undefined constant LOGFILE - assumed 'LOGFILE' in /in/COHN4 on line 6 Warning: error_log(LOGFILE): failed to open stream: Permission denied in /in/COHN4 on line 6 1
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/COHN4 on line 3
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/COHN4 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/COHN4 on line 2
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_CLASS in /in/COHN4 on line 2
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/COHN4 on line 2
Process exited with code 255.

preferences:
232.28 ms | 401 KiB | 352 Q