Home arrow PHP arrow Page 4 - Decoupling the Interaction Between Objects with Factory Methods

Putting the sample classes to work - PHP

Welcome to the fifth part of a six-part series on implementing factory methods in PHP 5. In this part, I show how the removal of a factory method inside the constructor of the class that originates persistent objects can produce a huge impact in the way that those objects handle its dependency, which happens to be a database handler object.

TABLE OF CONTENTS:
  1. Decoupling the Interaction Between Objects with Factory Methods
  2. Review: implementing a simple factory method in PHP 5
  3. Using a factory method more efficiently
  4. Putting the sample classes to work
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
December 14, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

If you’re like me, then it’s probable that you want to see how the modified version of the “User” class can be used to build a couple of persistent objects. Below I coded a script that does precisely that. Here it is:

// create first user object

$user1 = new User(MySQL::factory('host', 'user', 'password', 'database'));

$user1->name = 'Susan Norton';

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

 

// create second user object

$user2 = new User(MySQL::factory('host', 'user', 'password', 'database'));

$user2->name = 'Mary Smith';

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

Without a doubt, the relationship between the “User” class and the MySQL database handler is now much more independent. As you can see, the factory method of “MySQL” is used to inject a Singleton into the constructor, which demonstrates how simple it is to implement the injection dependency pattern via a constructor method.

In addition, the previous example can be streamlined even more, by coding it as follows:

// create instance of MySQL class via the factory method

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

// create first user object

$user1 = new User($db);

$user1->name = 'Susan Norton';

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

 

// create second user object

$user2 = new User($db);

$user2->name = 'Mary Smith';

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

There you have it. In this last case, the factory method of the “MySQL” class is called only once, and then the corresponding instance is simply inputted into the respective constructors of the persistent objects.

Finally, with this example I’m finishing this fifth tutorial of the series. As always, feel free to edit all of the code samples included in this article to arm yourself with a much better background in building factory methods with PHP 5.

Final thoughts

That’s about it for now. Over this fifth installment of the series, I showed how the removal of a factory method inside the constructor of the class that originates persistent objects can produce a huge impact in the way that those objects handle its dependency, which in this specific case happens to be a database handler object.

In the last chapter, I’m going to explain how to further improve the relationship established between the sample classes shown earlier, which surprisingly won’t require the use of a factory method.

Here’s my final suggestion: don’t miss the conclusion!



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