Home arrow PHP arrow Page 4 - Injecting Objects Using Setter Methods with the Dependency Injection Design Pattern

The dependency injection pattern in action - PHP

In this third article of a six-part series, you will learn how to implement the dependency injection pattern by using a simple setter method. This process is very similar to the one that uses a constructor, which we covered in the previous article.

TABLE OF CONTENTS:
  1. Injecting Objects Using Setter Methods with the Dependency Injection Design Pattern
  2. Review: implementing the dependency injection pattern using a constructor method
  3. Using dependency injection via a setter method
  4. The dependency injection pattern in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
October 14, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In the previous section you saw how easy it is to use a setter method for passing the MySQL database handler to the internals of the persistent “User” class. Nonetheless, the best way to understand this process is by example, right? Thus, below I coded one for you that demonstrates how to create two persistent objects in a snap.

Take a look at the following code fragment:

// create instance of MySQL class

$db = new MySQL('host', 'user', 'password', 'database');

// create first user object

$user1 = new User();

$user1->setDatabaseHandler($db);

$user1->load();

$user1->name = 'Alejandro Gervasio';

$user1->email = 'alejandro@domain.com';

 

// create second user object

$user2 = new User();

$user2->setDatabaseHandler($db);

$user2->load();

$user2->name = 'Mary Smith';

$user2->email = 'mary@domain.com'; 

Here you have it. Thanks to the implementation of dependency injection via a setter method, it’s extremely simple to build two trivial persistent objects. Also, it’s quite possible that you find the approach that utilizes a constructor a little bit cleaner, but when a class requires multiple dependencies, probably a setter method is a better option.

Finally, feel free to tweak all of the code samples shown in this article, so you can get a more intimate knowledge of applying the dependency injection pattern by means of a setter method.

Final thoughts

In this third article of the series, you hopefully learned how to implement the dependency injection pattern by using a simple setter method. As you saw for yourself, this process is very similar to the one that uses a constructor, so in theory you shouldn’t have major problems understanding its driving logic.

In the next chapter, things will get even more interesting. I plan to demonstrate how to use dependency injection within a PHP 5-based application that works with the popular Model-View-Controller pattern.

Don’t miss the forthcoming article!



 
 
>>> 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: