懒加载?最好避免吗? [英] Lazy loading? Is it better to avoid it?

查看:114
本文介绍了懒加载?最好避免吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



是否可以过度使用延迟加载来加载所有类,并忘记包括(..)完全

这种方法的缺点是什么?

解决方案

自动加载的几点:


  1. 通过使用自动加载
    ,总是包含所有
    文件,您将看到一个不错的表现
    所有的时间
    (特别是文件数量
    越来越大)


  2. 实现自动加载时,它是
    更好地使用
    spl_autoload_register() than
    __ autoload()


虽然很多时候,人们谈论在PHP中的懒惰加载,他们正在谈论的东西如下:

  class Foo {
protected $ bar = null;

public function getBar(){
if($ this-> bar == null){
$ this-> bar = ExpensiveOperation();
}
return $ this-> bar;
}
}

然后只有在实际需要时加载属性使用,而不是每次实例化对象,这可能有一些好的好处。


I just read about the "lazy loading" design pattern.

Is it ok to overuse lazy loading to load all classes and forget about include(..) entirely?
What are the cons of this approach?

解决方案

A couple points on autoloading:

  1. You will see a nice performance improvement by using autoloading versus always including all of your files all the time (especially as the number of files grows larger and larger).

  2. When implementing autoloading, it is better to use spl_autoload_register() than __autoload().

Although a lot of times when people talk about lazy loading in PHP, they are talking about something like the following:

class Foo {
    protected $bar = null;

    public function getBar() {
        if ($this->bar == null) {
            $this->bar = ExpensiveOperation();
        }
        return $this->bar;
    }
}

Then you only load a property when it actually needs to be used, and not every time you instantiate the object, which can potentially have some good benefits.

这篇关于懒加载?最好避免吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆