请帮助我识别我在这个php示例中使用的技术并为其命名

请帮助我识别我在这个php示例中使用的技术并为其命名,php,dependency-injection,lazy-loading,magic-methods,Php,Dependency Injection,Lazy Loading,Magic Methods,在工作中,我们发现这种获取相关数据/对象的方法简直太棒了 例如,我有一个类,里面有一些相关的对象: class Country { private $language; //$language will be an object of class Language private $regions; //$regions will be an ARRAY of Region objects //in the constructor i don't load regions or langua

在工作中,我们发现这种获取相关数据/对象的方法简直太棒了

例如,我有一个类,里面有一些相关的对象:

class Country {

private $language; //$language will be an object of class Language
private $regions; //$regions will be an ARRAY of Region objects

//in the constructor i don't load regions or language

//magic method
    public function __get($name) {

        $fn_name = 'get_' . $name;

        if (method_exists($this, $fn_name)) {
            return $this->$fn_name();
        } else {
            if (property_exists($this, $name))
                return $this->$name;
        }

        return $this->$name;
    }

    public function get_language () {

        if (is_object($this->language)) return $this->language;

        $this->language = new Language($params); //example

        return $this->language;
    }

    public function get_regions () {

        if (is_array($this->regions)) return $this->regions;

        $this->regions = array();

        $this->regions[] = new Region('fake');
        $this->regions[] = new Region('fake2');

        return $this->regions;
    }
}
因此,我们的想法是:

我想要一个国家的例子,但我现在不需要它的语言和地区

在另一种情况下,我需要它们,因此我将它们声明为属性,而magic方法仅在第一次为我检索它们

$country = new Country();

echo "language is". $country->language->name;

echo "this country has ". sizeof($country->regions)." regions";
这个按需方法(也避免了相关对象的嵌套循环)有一个名称? 可能是延迟加载属性?按需房地产

在另一种情况下,我需要它们,[…]仅在第一次为我检索它们

初始化将是正确的措辞。这称为延迟初始化

所以我将它们声明为属性,然后magic方法检索它们

这称为属性重载

编辑:

我不认为有一个术语是指两者的结合。您可以将两者结合起来,得到类似“通过重载延迟初始化属性”的结果

在另一种情况下,我需要它们,[…]仅在第一次为我检索它们

初始化将是正确的措辞。这称为延迟初始化

所以我将它们声明为属性,然后magic方法检索它们

这称为属性重载

编辑:


我不认为有一个术语是指两者的结合。您可以将二者结合起来,得到类似“通过重载延迟初始化属性”的结果。

“延迟加载”是常用名称。“延迟加载”是常用名称