<?php
class App {
public static $classes = array();
public static function register($name, Callable $generator) {
$classes[$name] = $generator;
}
public static function make($name) {
if (!isset($classes[$name])) {
throw new Exception('Invalid class name');
}
return ($classes[$name])();
}
}
// Internal declaration
class Foo {
public static function bar() {
return 'static';
}
}
App::register('foo', function() {
return new Foo;
});
// Internal Usage
class SomeController {
public function view() {
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();
Fatal error: Uncaught Exception: Invalid class name in /in/vLXDG:13
Stack trace:
#0 /in/vLXDG(41): App::make('foo')
#1 /in/vLXDG(63): SomeController->view()
#2 {main}
thrown in /in/vLXDG on line 13
Process exited with code 255.
Parse error: syntax error, unexpected '(' in /in/vLXDG on line 16
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '(' in /in/vLXDG on line 16
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/vLXDG on line 5
Process exited with code 255.
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/vLXDG on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/vLXDG on line 5
Process exited with code 255.