Home arrow PHP arrow Page 4 - Dependency Injection: Using a Setter Method within a Model

Performing CRUD operations on user-related records - PHP

In this fifth part of a six-part series on the dependency injection pattern, I explore the implementation of the pattern by using a simple setter method within a model class. You'll see that this approach is very easy to follow.

TABLE OF CONTENTS:
  1. Dependency Injection: Using a Setter Method within a Model
  2. Review: the Model-View-Controller and Dependency Injection Patterns
  3. Applying dependency injection through a setter method
  4. Performing CRUD operations on user-related records
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
October 19, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I said in the preceding segment, I’d like to finish this tutorial by showing you how the refactored version of the previous “UserModel” class can be used for inserting, updating and deleting records from its associated “users” MySQL table.

To do that, I coded a short script that shows how neatly dependency injection works when using the model’s setter method. Here’s the corresponding example code:

// create new instance of MySQL class

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

$userModel = new UserModel();

// inject MySQL instance in the model via its setter method

$userModel->setDatabaseHandler($db);

// add new user

$userModel->save(array('fname' => 'Alejandro', 'lname' => 'Gervasio', 'email' => 'alejandro@domain.com'));

// update existing user

$userModel->save(array('fname' => 'Mary', 'lname' => 'Wilson', 'email' => 'mary@domain.com'), 1);

// delete existing user

$userModel->delete(1);

As seen above, once an instance of the “MySQL” class has been properly created, it’s injected into the internals of the user model via the “setDataBaseHandler()” method, so it can perform CRUD operations against its targeted table.

Again, it’s fair to point out that the approach that you’ll use to apply dependency injection within your own PHP 5-based applications will depend on certain factors, including the number of dependencies required by class, the application's structure, and finally, your personal coding preferences.

Final thoughts

In this fifth episode of the series, I explored the implementation of the dependency injection pattern by using a simple setter method within a model class. As you were able to see for yourself, this approach is very easy to follow, so I think you shouldn’t have major problems putting it into practice.

In addition, it’s worthwhile to say that another common case where dependency injection shines is when building polymorph objects via interfaces. That sounds hard to grasp, right? Well, fear not, because in the following article I’m going to teach you how to apply this useful concept to building a couple of database drivers in PHP 5.

Don’t miss the next 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: