Home arrow PHP arrow Page 3 - Injecting Objects by Constructor with the Dependency Injection Pattern

Applying the Inversion of Control principle - PHP

While not being as widely known as other design patterns such as Factory, Singleton or Active Record, the Dependency Injection pattern provides both programmers and web developers with a well-structured solution that allows them to solve issues that arise when an object needs the functionality of another one (hence the dependency) to work as expected. This is the second part of a six-part series that shows you how to apply this pattern.

TABLE OF CONTENTS:
  1. Injecting Objects by Constructor with the Dependency Injection Pattern
  2. Review: inefficiently handling class dependencies
  3. Applying the Inversion of Control principle
  4. The dependency injection pattern in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
October 13, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I explained in the section that you just read, it’s extremely simple to invert the roles played by the classes that you saw before, thanks to the functionality given by dependency injection. In this particular case, I’m going to implement only a specific flavor of it, where the dependency of a class is passed in through a constructor.

To show you the benefits of dependency injection, the only thing that I’m going to do next is modify the constructor of the persistent “User” class, which now will look as follows:

class User

{

private $data = array();

private $id = NULL;

private $db = NULL;

 

// constructor

public function __construct(MySQL $db, $id = NULL)

{

$this->db = $db;

if ($id !== NULL)

{

$this->id = $id;

$this->db->query('SELECT * FROM users WHERE id=' . $this->id);

$this->data = $this->db->fetch();

}

}

 

// set undeclared property

public function __set($property, $value)

{

if ($property !== 'name' and $property !== 'email')

{

return;

}

$this->data[$property] = $value;

}

 

// get undeclared property

public function __get($property)

{

if (isset($this->data[$property]) === TRUE)

{

return $this->data[$property];

}

}

 

// save object to session variable

public function __destruct()

{

if ($this->id === NULL)

{

$this->db->query("INSERT INTO users (id, name, email) VALUES (NULL, '$this->name', '$this->email')");

}

else

{

$this->db->query("UPDATE users SET name = '$this->name', email = '$this->email' WHERE id = $this->id");

}

}

}

Didn’t I tell you that implementing the dependency injection pattern was a simple process? Well, the tweaked definition of the “User” class proves that without a doubt. As you can see, now the class’s constructor is no longer responsible for creating an instance of the database handler; it simply takes it smoothly as an incoming argument.

It’s probable that you’ve already coded your classes that way, even without knowing that you were using the benefits of dependency injection. Now you know it, and you should feel proud of it.

Returning to the definition of the “User” class, since its dependency is passed to its internals via the constructor, the process, not surprisingly, is known as “dependency injection by constructor.” But there are other ways to achieve the same result. However, for the moment I’m going to explore only this introductory approach.

So far, so good. At this point, you’ve hopefully grasped the underlying logic of the dependency injection pattern, meaning that it’s time to see how the proper implementation of this pattern can improve the way that persistent objects are created.

Thus, in the last segment of this tutorial I’m going to set up a final example for you. It will show how the modified version of the “User” class will now behave much more efficiently when it comes to handling its dependency.

To see how this example will be developed, click on the link below and keep reading.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: