Home arrow PHP arrow Page 4 - Applying the Dependency Injection Pattern and Factory Methods in PHP 5

Creating persistent objects without a factory method - PHP

Welcome to the final part of a series on implementing factory methods in PHP 5. Composed of six approachable tutorials, this series uses numerous hands-on examples to teach you how to define specific methods within your own PHP programs. These methods return to client code instances from their originating classes, and from other classes as well.

TABLE OF CONTENTS:
  1. Applying the Dependency Injection Pattern and Factory Methods in PHP 5
  2. Review: the classes so far
  3. Removing the factory method from the MySQL class
  4. Creating persistent objects without a factory method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
December 15, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I expressed before, Id like to conclude this tutorial by showing you how to build a couple of persistent objects (or more, if you want to) even though the factory method of the MySQL abstraction class has been removed. 

To prove that, heres another script that shows how to perform this task, assuming that the definitions of the corresponding sample classes have been previously included. Take a look at it:  

// create instance of MySQL class and inject it into the user class constructor

$db = new MySQL('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'

As shown above, an instance of the MySQL class has been injected directly into the constructor of each persistent object. Naturally, the most important thing to stress here is that this was achieved without calling any factory method. Pretty neat, huh?

This example demonstrates how an adequate implementation of the Dependency Injection pattern allows you to pass an instance of a given class to other objects without using factories. Of course, this is valid only when the dependency is injected via a constructor or setter method, but if a dependency container is required, then coding a factory method that returns Singletons surely will be a good option.

Final thoughts

Its hard to believe, but weve come to the end of this series. Hopefully the experience has been educational for you, since you learned the basics of defining and implementing factory methods in PHP 5.

As you saw from the examples developed in the subsequent tutorials, a proper use of the Factory and Singleton patterns can be a powerful combination that allows you to write more robust and efficient applications. Think of them the next time that you need to build a truly killer PHP program.

See you in the upcoming PHP development tutorial!



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