dongyun3897 2015-01-31 14:58
浏览 20
已采纳

为什么[]表示法用于将元素输入到静态数组[复制]

This question already has an answer here:

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();
</div>
  • 写回答

1条回答 默认 最新

  • dsshsta97935 2015-01-31 15:07
    关注

    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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器