3v4l.org

run code in 300+ PHP versions simultaneously
<?php class App { public static $classes = array(); public function register($name, Callable $generator) { $this::classes[$name] = $generator; } public function make($name) { if (!isset($this::classes[$name])) { throw new Exception('Invalid class name'); } return $this::classes[$name](); } } $app = new App; // Internal declaration class Foo { public static function bar() { return 'static'; } } $app->register('foo', function() { return new Foo; }); // Internal Usage class SomeController { public function view() { $app = new App; echo $app->make('foo')->bar(); } } // Some extending application class MyFoo extends Foo { public static function bar() { return 'can\'t make internal methods use this.'; } } $app->register('foo', function() { return new MyFoo; }); $c = new SomeController; $c->view();
Output for 5.3.0 - 5.3.28, 5.4.0 - 5.4.27
Parse error: syntax error, unexpected '[' in /in/v7HXK on line 9
Process exited with code 255.

preferences:
187.87 ms | 1395 KiB | 64 Q