Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The Keywords you are looking for are <strong>eager loading</strong> vs <strong>lazy loading</strong>.</p> <p>In Short: </p> <ul> <li>Eager Loading is what you are doing at the Moment: Once an object is created, you are loading all related objects and attributes, no matter how Long it takes.</li> <li>Lazy loading is the opposite: There you will ONLY load Information, when it is required. (The Moment, it is really accessed)</li> </ul> <p>--</p> <p>A (very Basic) implementation of both would look like the example bellow.</p> <pre><code>//Data Model abstract class UserModel{ protected $userData = null; protected $userPosts = null; protected function loadUserData(){ //do whatever required and store in $result $this-&gt;userData = $result; } protected function loadUserPosts(){ //do whatever required and store in $result $this-&gt;userPosts = $result; } public abstract function getUserData(); public abstract function getUserPosts(); } //Eager Loading class EagerUserModel extends UserModel { public function __construct() { $this-&gt;loadUserData() $this-&gt;loadUserPosts(); } public function getUserData(){ return $this-&gt;userData; } public function getUserPosts(){ return $this-&gt;userPosts; } } //Lazy Loading class LazyUserModel extends UserModel { public function __construct() { //do nothing } public function getUserData(){ if ($this-&gt;userData == null){ $this-&gt;loadUserData(); } return $this-&gt;userData; } public function getUserPosts(){ if ($this-&gt;userPosts== null){ $this-&gt;loadUserPosts(); } return $this-&gt;userPosts; } } </code></pre> <p>The Example will allow BOTH ways. However you could implement either eager or lazy loading within a single class, if you dont want to have the "choice", of which type to use.</p> <p>Eager Loading has the Advantage that EVERY Information is "just there". Lazy Loading however requires a more complex architecture. To load the "UserPosts", you might require additional data about the user, which means you have to load the UserData first. This is something you Need to take into account!</p> <p><strong>So, Lazy loading is always faster?</strong></p> <p>No! That's the pitfall. Imagine, you have a class with 10 Attributes. If you are loading every Attribute in a lazy way, that would require 10 SQL-Queries to be fired (<code>SELECT Name FROM user...</code>, <code>SELECT email FROM user...</code> and so on). Doing this in an Eager way, would allow you to run only ONE Query: <code>Select Name, email FROM user...</code>.</p> <p>You have to find the Balance between both methods. Are Foreign Objects tightly Coupled? (I.e. user &lt;-> Group)? -> Load Eager. Are foreign objects loosely coupled (User -> Posts on Image 545458) -> Load lazy.</p> <p>Also Note, that this is an <em>extreme</em> example (100% eager vs 100% lazy). In practice, you may want to load some things eager (user data, Group allocation), and others lazy (comments, Group permissions) - You cant create a own Extension of the base class for every usecase. However having a "BaseClass" is always a good idea, because it gives you flexibility, whenever another implementation is required.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload