-1

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();
AL-zami
  • 8,902
  • 15
  • 71
  • 130
  • 1
    When you removed `static` from definition of `$methods`, it can no longer be found by static `registerMethods`. That's what the error is about. – Sergio Tulentsev Jan 31 '15 at 15:00
  • 1
    `[]` appends a new value onto an array (_any_ array - unrelated to static properties) (and creates the array if it doesn't exist) http://php.net/manual/en/language.types.array.php That is unrelated to your error - the error is because `self::$methods` refers to a static property, but having removed the `static` keyword from its declaration, you turned it into an instance property. – Michael Berkowski Jan 31 '15 at 15:01
  • If you want to use it as an instance property instead of a static one, you'd need to use `$this->methods`, but that won't work inside the static method `registerMethod()`. – Michael Berkowski Jan 31 '15 at 15:02
  • @MichaelBerkowski but this array already declared.Then why i need to use [] notation ? – AL-zami Jan 31 '15 at 15:03
  • @al-Zami Look at the examples in the docs I linked. `[]` does not _declare_ an array (though it can), it _adds values onto_ an array. It being created if not already existing is a side effect. It's like an alternative to `array_push()`, though possibly faster. – Michael Berkowski Jan 31 '15 at 15:04
  • is it something similar to push method in javascript? – AL-zami Jan 31 '15 at 15:05
  • You need to read a PHP tutorial. Why would you remove the `static` keyword if you don't know what it does, and then be surprised that the program broke? – user229044 Jan 31 '15 at 15:06
  • Yes, it is like the `.push()` method in JS, or `array_push()` in PHP. http://codepad.viper-7.com/usvFwy – Michael Berkowski Jan 31 '15 at 15:07

1 Answers1

0

what is this [ ] notation is used for and how it is related to static keyword??

The use of static and [] are unrelated.

The code $array[] = $i is a shorthand for pushing the element $i onto the end of the array $array. It could also be written array_push($array, $i).

user229044
  • 232,980
  • 40
  • 330
  • 338