in the original example $methods
is a static array.It is used to assign outsider functions as methods to a class.But to input new element to this array writer used the following expression
static protected $methods = array();
i removed the static keyword from the variable declaration and tried to execute the code.
protected $methods = array();
the execution gave the following error:
Fatal error: Access to undeclared static property: Dynamic::$methods in C:\xampp\htdocs\practice\json.php on line 6
what is this [ ] notation is used for and how it is related to static keyword??
original full code:
class Dynamic {
static protected $methods = array();
public static function registerMethod($method) {
self::$methods[] = $method;
}
private function __call($method, $args) {
if (in_array($method, self::$methods)) {
return call_user_func_array($method, $args);
}
}
}
function test() {
print "Hello World" . PHP_EOL;
}
Dynamic::registerMethod('test');
$d = new Dynamic();
$d->test();