首页  >  问答  >  正文

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 天前997

全部回复(1)我来回复

  • 三叔

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

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

    回复
    0
  • 取消回复