Home arrow PHP arrow Page 4 - Using Database Objects with Factory Methods

The factory method in action - PHP

In this fourth part of a six-part series on implementing factory methods in PHP 5, I show how a simple factory method can improve the efficiency of a sample database-driven application when a database handler is utilized by multiple objects. In this specific case, the method returns Singletons of the database handler, which is a significant enhancement.

TABLE OF CONTENTS:
  1. Using Database Objects with Factory Methods
  2. Sharing a database connection handler between different objects
  3. Defining a simple factory method
  4. The factory method in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
December 08, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Now that you hopefully grasped how the two sample classes defined in the previous segment are capable of interacting with each other, itís time to set up an example that shows them in action. Therefore, I suggest you look at the following script. It demonstrates how to create two persistent user objects that share the same database handler, thanks to the functionality of the factory method. Here it is:  

// create first user object

$user1 = new User();

$user1->name = 'Susan Norton';

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

 

// create second user object

$user2 = new User();

$user2->name = 'Mary Smith';

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

Even though this code fragment looks similar to the one shown in the first section of this tutorial, the truth is that things are quite different behind the scenes. In that first case, each user object created a different instance of the database handler, while in this last example, only one instance of it is shared between those objects.

While this code sample is rather trivial in its nature, it demonstrates how the definition of a factory method that returns Singletons may help to implement a more effective relationship between some objects and the respective dependencies.

Finally, if you wish to tweak all of the examples included in this article, either for educational purposes or just for fun, feel free to do that. The experience will be instructive, trust me.

Final thoughts

In this fourth installment of the series, I showed how a simple factory method can improve the efficiency of a sample database-driven application when a database handler is utilized by multiple objects. In this specific case, the method in question returned Singletons of the database handler, which is a significant enhancement.

Nevertheless, this example application in its current version is far from being really efficient. The constructor of the previous ďUserĒ class is still responsible for creating an instance of the database handler, which you know is a bad thing. Thus, in the next tutorial Iím going to decouple this process, so the involved classes will be able to work more independently.

Donít miss the upcoming 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: