title: PHP Lazy loading objects and dependency injection
body: <p>I have recently started reading about dependency injection and it has made me rethink some of my designs.</p>
  
  <p>The problem i have is something like this:
  Let's say i have two classes: Car and Passenger;
  For those two classes i have some data mappers to work with the database: CarDataMapper and PassengerDataMapper</p>
  
  <p>I want to be able to do something like this in code:</p>
  
  <pre><code>$car = CarDataMapper-&gt;getCarById(23); // returns the car object
  foreach($car-&gt;getPassengers() as $passenger){ // returns all passengers of that car
      $passenger-&gt;doSomething();
  }
  </code></pre>
  
  <p>Before I knew anything about DI, I would build my classes like this:</p>
  
  <pre><code>class Car {
      private $_id;
      private $_passengers = null;
  
      public function getPassengers(){
          if($this-&gt;_passengers === null){
              $passengerDataMapper = new PassengerDataMapper;
              $passengers = $passengerDataMapper-&gt;getPassengersByCarId($this-&gt;getId());
              $this-&gt;setPassengers($passengers);
          }
          return $this-&gt;_passengers;  
      }
  
  }
  </code></pre>
  
  <p>I would also have similar code in the Passenger->getCar() method to fetch the car the passenger is in.</p>
  
  <p>I now understand that this creates dependencies (well, I understood it before too, but I wasn't aware that this is "wrong") between the Car and the Passenger objects and the data mapper objects.</p>
  
  <p>While trying to think of the solution for this two options came to mind, but I don't really like any of them:</p>
  
  <p>1: Doing something like this:</p>
  
  <pre><code>$car = $carDataMapper-&gt;getCarById(23);
  $passengers = $passengerDataMapper-&gt;getPassengersByCarId($car-&gt;getId());
  $car-&gt;setPassengers($passengers);
  
  foreach($car-&gt;getPassengers() as $passenger){
      $passenger-&gt;doSomething();
  }
  </code></pre>
  
  <p>But what if passengers have objects that they need injected, and what if the nesting goes to ten or twenty levels... I would wind up instantiating nearly every object in the start of my application, which would in turn query the entire database during the process.
  If i have to send the passenger to another object which has to do something with the objects that the passenger holds, I do not want to immediately instantiate these objects too.</p>
  
  <p>2: Injecting the data mappers into the car and passenger objects and having something like this:</p>
  
  <pre><code>class Car {
      private $_id;
      private $_passengers = null;
      private $_dataMapper = null;
  
      public function __construct($dataMapper){
          $this-&gt;setDataMapper($dataMapper);
      }
  
      public function getPassengers(){
          if($this-&gt;_passengers === null &amp;&amp; $this-&gt;_dataMapper instanceof PassengerDataMapper){
              $passengers = $this-&gt;_dataMapper-&gt;getPassengersByCarId($this-&gt;getId());
              $this-&gt;setPassengers($passengers);
          }
          return $this-&gt;_passengers;  
      }
  }
  </code></pre>
  
  <p>I dont like this any better, because it's not like the Car is really unaware of the data mapper, and without the data mapper, the Car could behave unpredictably (not returning passengers, when it actually has them)</p>
  
  <p>So my first question is: 
  Am I taking a completely wrong approach here, because, the more I look at it, the more it looks like I'm building an ORM, instead of a business layer?</p>
  
  <p>The second question is: 
  is there a way of actually decoupling the objects and the data mappers in a way that would allow me to use the objects as described in the very first code block? </p>
  
  <p>Third question:
  I've seen some answers for other languages (some version of C, I think) resolving this issue with something like this described here:
  <a href="https://stackoverflow.com/questions/130292/what-is-the-proper-way-to-inject-a-data-access-dependency-for-lazy-loading">What is the proper way to inject a data access dependency for lazy loading?</a>
  As I haven't had time to play with other languages, this makes no sense to me, so I'd be grateful if someone would explain the examples in the link in PHP-ish.</p>
  
  <p>I have also looked at some DI frameworks, and read about DI Containers and Inversion of Control, but from what I understood they are used to define and inject dependencies for 'non dynamic' classes, where for instance, the Car would depend on the Engine, but it would not need the engine to be loaded dynamically from the db, it would simply be instantiated and injected into the Car.</p>
  
  <p>Sorry for the lengthy post and thanks in advance.</p>