php - Instantiate an object with only some of its members -
i'm working in php application, , i'd able instantiate object, need instantiated of it's properties, not of them. eg.
class user { public function __construct() { $this->user_id = 0; $this->name = ''; $this->profile = array(); //...many other members here } }
every time instantiate object brings many collections of data, (for example, brings it's "profile" properties , on). , not wanted behavior because, let's need use name of user, why having in memory rest of properties? in other cases, need them right away.
some ideas:
i create class extends user, , in constructor method unset unwanted properties. however, i'm looking more reusable way, can same other objects in application.
i take properties out of constructor method, force me change core of application (classes above user class), , alter many things in application.
is there more reusable way using standard intermediary class or design pattern? thank you.
the keywords looking eager loading vs lazy loading.
in short:
- eager loading doing @ moment: once object created, loading related objects , attributes, no matter how long takes.
- lazy loading opposite: there load information, when required. (the moment, accessed)
--
a (very basic) implementation of both example bellow.
//data model abstract class usermodel{ protected $userdata = null; protected $userposts = null; protected function loaduserdata(){ //do whatever required , store in $result $this->userdata = $result; } protected function loaduserposts(){ //do whatever required , store in $result $this->userposts = $result; } public abstract function getuserdata(); public abstract function getuserposts(); } //eager loading class eagerusermodel extends usermodel { public function __construct() { $this->loaduserdata() $this->loaduserposts(); } public function getuserdata(){ return $this->userdata; } public function getuserposts(){ return $this->userposts; } } //lazy loading class lazyusermodel extends usermodel { public function __construct() { //do nothing } public function getuserdata(){ if ($this->userdata == null){ $this->loaduserdata(); } return $this->userdata; } public function getuserposts(){ if ($this->userposts== null){ $this->loaduserposts(); } return $this->userposts; } }
the example allow both ways. implement either eager or lazy loading within single class, if dont want have "choice", of type use.
eager loading has advantage every information "just there". lazy loading requires more complex architecture. load "userposts", might require additional data user, means have load userdata first. need take account!
so, lazy loading faster?
no! that's pitfall. imagine, have class 10 attributes. if loading every attribute in lazy way, require 10 sql-queries fired (select name user...
, select email user...
, on). doing in eager way, allow run 1 query: select name, email user...
.
you have find balance between both methods. foreign objects tightly coupled? (i.e. user <-> group)? -> load eager. foreign objects loosely coupled (user -> posts on image 545458) -> load lazy.
also note, extreme example (100% eager vs 100% lazy). in practice, may want load things eager (user data, group allocation), , others lazy (comments, group permissions) - cant create own extension of base class every usecase. having "baseclass" idea, because gives flexibility, whenever implementation required.
Comments
Post a Comment