Maison  >  Questions et réponses  >  le corps du texte

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 Il y a quelques jours1003

répondre à tous(1)je répondrai

  • 三叔

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

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

    répondre
    0
  • Annulerrépondre