dependency injection - Help me to recognize the technique i use in this php example and naming it -
ok @ work have discovered method fetch related data / objects near awesome.
right example, have class related objects inside:
class country { private $language; //$language object of class language private $regions; //$regions array of region objects //in constructor 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; } }
so idea is:
i want instance of country, dont need language , regions now.
in case need them, claim them properties, , magic method retrieves them me first time.
$country = new country(); echo "language is". $country->language->name; echo "this country has ". sizeof($country->regions)." regions";
this on-demand method (that avoids nested loop of related objects too) has name? maybe lazy loading properties? on-demand properties?
in case need them, [...] retrieves them me only first time.
initialize proper wording. called lazy initialization.
http://en.wikipedia.org/wiki/lazy_initialization
so claim them properties, , magic method retrieves them
this called overloading of properties.
edit:
i don't think there's term combination of two. can combine both , "lazy initialization of properties through overloading".
Comments
Post a Comment