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); 有什么优势?
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); 有什么优势?惰加载。
这两种写法都可以,但是。
$this['config'] = new Config($config);
这种方式,当你给$this->['config']赋值的时候,即进行了new Config($config)操作。
$this['config'] = function () use ($config) { return new Config($config);}这种方式,你只是给$this->['config']一个匿名函数,当你要用到的时候,才会进行new Config($config)的操作。
不知道我这种解释对不对= =比较不善表达= =能不能直接写成这样:$this['config'] = new Config($config);有什么优势?完全可以写成这样,只不过每次在实例化的时候都会去new Config这个类,并不管用不用的到;$this['config'] = function () use ($config) { return new Config($config);}这种写法呢,是给$this['config']声明了一个匿名函数,当$this['config']被真正调用的时候才会去new Config这个类;这样写的好处的是,当$this['config']不被真正使用时,减少了额外实例化的过程和内存的消耗closure会在真正调用的时候才new一个Config, 这样就可以实现了lazy load。
除了上面的懒加载, 还有一个好处是实现了一个工厂模式 -- 每次拿config都是新new出来的1、懒加载大家都说到了2、其实匿名函数很大程度上函数式编程的一个体现。
答:详情>>
问:用电脑时可以将网络转换成无线网吗?貌似有这种技术……请高手指路~[围观]
答:可以。 准备无线路由器一个,将入线接入Wan口,通过Lan口设置好自动拨号,并打开无线SSID设置好密码。路由器自动拨号连接网络后,通过WiFi接入网络即可。 ...详情>>
答:控制面板中有一个“网络安装向导”,按照它的提示就能完成了详情>>