3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class Arr { public static function collapse($array, $prefix = '') { $output = []; foreach($array as $key => $value) { if (Assert::that($value)->array()->result()) { static::merge($output, static::collapse($value, $prefix . $key . '.')); } else { } } return $output; } public static function merge(...$arrays) { Assert::that($arrays)->items('array')->error(); $output = []; foreach($arrays as $array) { $output = array_merge($output, $array); } return $output; } public static function join($glue = '', $array = []) { return implode($glue, $array); } } class Funct { public static function getCaller($offset = 0, $full = false) { return static::nameFromBacktrace(debug_backtrace()[2 + $offset], $full); } public static function nameFromBacktrace($backtrace, $full = false) { if($full) { if(!array_key_exists('type', $backtrace)) return $backtrace['function']; if($backtrace['type'] === '::') { return $backtrace['class'] . '::' . $backtrace['function']; } elseif($backtrace['type'] === '->') { return [$backtrace['class'], $backtrace['function']]; } } return $backtrace['function']; } } class Assert { protected $variable; protected $invalidated = false; protected $invalidatedBy = []; protected function __construct($variable) { $this->variable = $variable; } public static function that($variable) { return new static($variable); } public function string() { return $this->check('is_string'); } public function array() { return $this->check('is_array'); } public function object() { return $this->check('is_object'); } public function boolean() { return $this->check('is_bool'); } public function callable() { return $this->check('is_callable'); } public function integer() { return $this->check('is_int'); } public function null() { return $this->check('is_null'); } public function float() { return $this->check('is_float'); } public function double() { return $this->check('is_double'); } public function resource() { return $this->check('is_resource'); } public function type($type) { return $this->check('is_a', $type); } public function items($type) { return $this->checkAll('is_a', $type); } protected function check(callable $function, ...$parameters) { array_unshift($parameters, $this->variable); if(!call_user_func_array($function, $parameters)) $this->invalidate(); return $this; } protected function checkAll(callable $function, ...$parameters) { foreach($this->variable as $value) { array_unshift($parameters, $value); if(!call_user_func_array($function, $parameters)) $this->invalidate(); break; } return $this; } public function result() { return !$this->invalidated; } public function error() { if(!$this->result()) throw new \AssertionError("The assertion failed at: " . Arr::join(', ', $this->invalidatedBy)); } protected function invalidate($by = null) { $this->invalidated = true; $this->invalidatedBy[] = Funct::getCaller(1); } }
Output for 7.0.0 - 7.2.0
Output for 5.6.8 - 5.6.28
Parse error: syntax error, unexpected 'array' (T_ARRAY), expecting identifier (T_STRING) in /in/eY20g on line 78
Process exited with code 255.
Output for 5.4.10 - 5.5.35
Parse error: syntax error, unexpected '.', expecting '&' or variable (T_VARIABLE) in /in/eY20g on line 21
Process exited with code 255.
Output for 5.3.20 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/eY20g on line 7
Process exited with code 255.