3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Validator { const GREATER = [self::class, 'greater']; const IS_TYPE = [self::class, 'isType']; public static function all(...$rules) { foreach ($rules as $def) { $rule = array_shift($def); if ($rule(...$def)) { continue; } return false; } return true; } public static function greater($value, $compare) : bool { return $value > $compare; } public static function isType($value, $type) : bool { return gettype($value) === $type; } } class Model { const RULES = [ 'foo' => [ 'integer', // shorthand for [Validator::IS_TYPE, 'integer'] [Validator::GREATER, 10] ] ]; protected $foo; public function set($name, $value) { if ($this->isValid($name, $value)) { $this->$name = $value; return; } throw new Exception("{$name} cannot be {$value}"); } function isValid() : bool { $rules = []; foreach (self::RULES[$name] as $rule) { // convenience: turn strings into typehints if (is_string($rule)) { $rules[] = [Validator::IS_TYPE, $value]; continue; } // ... other convenience parsing ... // for fully defined rules, just add value arg at proper spot array_splice($rule, 1, 0, $value); $rules[] = $rule; } return Validator::ALL($rules); } } $m = new Model; $m->set('foo', 15); var_dump($m); $m->set('foo', 7);
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.4, 8.3.6
Warning: Undefined variable $name in /in/OXc1r on line 47 Warning: Undefined array key "" in /in/OXc1r on line 47 Warning: foreach() argument must be of type array|object, null given in /in/OXc1r on line 47 Fatal error: Uncaught Error: Value of type null is not callable in /in/OXc1r:11 Stack trace: #0 /in/OXc1r(62): Validator::all(Array) #1 /in/OXc1r(38): Model->isValid('foo', 15) #2 /in/OXc1r(68): Model->set('foo', 15) #3 {main} thrown in /in/OXc1r on line 11
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 Warning: Undefined variable $name in /in/OXc1r on line 47 Warning: Undefined array key "" in /in/OXc1r on line 47 Warning: foreach() argument must be of type array|object, null given in /in/OXc1r on line 47 Fatal error: Uncaught Error: Value of type null is not callable in /in/OXc1r:11 Stack trace: #0 /in/OXc1r(62): Validator::all(Array) #1 /in/OXc1r(38): Model->isValid('foo', 15) #2 /in/OXc1r(68): Model->set('foo', 15) #3 {main} thrown in /in/OXc1r on line 11
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.25, 7.4.27 - 7.4.33
Notice: Undefined variable: name in /in/OXc1r on line 47 Notice: Undefined index: in /in/OXc1r on line 47 Warning: Invalid argument supplied for foreach() in /in/OXc1r on line 47 Fatal error: Uncaught Error: Function name must be a string in /in/OXc1r:11 Stack trace: #0 /in/OXc1r(62): Validator::all(Array) #1 /in/OXc1r(38): Model->isValid('foo', 15) #2 /in/OXc1r(68): Model->set('foo', 15) #3 {main} thrown in /in/OXc1r on line 11
Process exited with code 255.
Output for 7.3.32 - 7.3.33, 7.4.26
Warning: Invalid argument supplied for foreach() in /in/OXc1r on line 47 Fatal error: Uncaught Error: Function name must be a string in /in/OXc1r:11 Stack trace: #0 /in/OXc1r(62): Validator::all(Array) #1 /in/OXc1r(38): Model->isValid('foo', 15) #2 /in/OXc1r(68): Model->set('foo', 15) #3 {main} thrown in /in/OXc1r on line 11
Process exited with code 255.

preferences:
167.89 ms | 402 KiB | 187 Q