3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function struct(string $name, array $class) { $class = json_encode($class); define($name, $class); } function create($class) { $object = (array) json_decode($class); //if(function_exists($object['__construct']())) $object['__construct']($object); return $object; } function call(array &$class, string $member, ...$args) { if(!isset($class[$member])) exit($member.' is not a member of the class.'); else { if(!is_callable($class[$member])) return $class[$member]; else { if(!isset($args[0])) $class[$member]($class); else $class[$member]($class, $args); } } } struct('myStruct', [ 'foo' => 'Hello World!', 'bar' => function (&$this) { echo(' Hi!'); } ]); $obj = create(myStruct); echo(call($obj, 'foo')); call($obj, 'bar'); echo(call($obj, 'foo'));
based on Zh3XN
Output for 7.0.7 - 7.2.0
Fatal error: Cannot use $this as parameter in /in/pJOVg on line 24
Process exited with code 255.
Output for 7.0.0 - 7.0.6
Hello World!Hello World!
Output for 5.6.0 - 5.6.28
Catchable fatal error: Argument 1 passed to struct() must be an instance of string, string given, called in /in/pJOVg on line 27 and defined in /in/pJOVg on line 2
Process exited with code 255.
Output for 5.5.0 - 5.5.37
Parse error: syntax error, unexpected '.', expecting '&' or variable (T_VARIABLE) in /in/pJOVg on line 11
Process exited with code 255.