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 git.master, git.master_jit, rfc.property-hooks
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.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
38.73 ms | 401 KiB | 8 Q