>  Q&A  >  본문

php 闭包的作用

  public function __construct($config)
    {    parent::__construct();    $this['config'] = function () use ($config) {        return new Config($config);
    };
    ...
    其中    $this['config'] = function () use ($config) {        return new Config($config);
    能不能直接写成这样:    $this['config'] = new Config($config);
    有什么优势?


高洛峰高洛峰2883일 전998

모든 응답(1)나는 대답할 것이다

  • 三叔

    三叔2016-11-07 09:58:41

    closure会在真正调用的时候才new一个Config, 这样就可以实现了lazy load.

    회신하다
    0
  • 취소회신하다