Home arrow PHP arrow Page 3 - The Dependency Injection Design Pattern in PHP 5

Defining a basic factory method - PHP

In this first part of a six-part series, I introduce you to the dependency injection design pattern and its use with MySQL. Specifically, I create a typical scenario where one persistent class needs the functionality of its dependency, in this case a database handler, to gain access to a MySQL table.

TABLE OF CONTENTS:
  1. The Dependency Injection Design Pattern in PHP 5
  2. A basic MySQL-driven application with the dependency injection pattern
  3. Defining a basic factory method
  4. Changing the definition of the User class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
October 08, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Frankly speaking, the best way to define a relationship between the previous “User” class and its dependency, that is the database handler, is by applying the dependency injection pattern. Nonetheless, at least for the moment I’m going to introduce a couple of modifications to these classes, so they can interact with each other in a more efficient manner.

First, the “MySQL” class will expose a brand new method, called “factory(),” which will return to client code a Singleton instance of the class.

The improved definition of this class now looks as follows:

class MySQL

{

private $result = NULL;

private $link = NULL;

private static $instance = NULL;

 

// returns a singleton instance of MySQL class (chainable)

public static function factory($host, $user, $password, $database)

{

if (self::$instance === NULL)

{

self::$instance = new MySQL($host, $user, $password, $database);

}

return self::$instance;

}

// connect to MySQL

public function __construct($host, $user, $password, $database)

{

if (FALSE === ($this->link = mysqli_connect($host, $user, $password, $database)))

{

throw new Exception('Error : ' . mysqli_connect_error());

}

}

 

// perform query

public function query($query)

{

if (is_string($query) AND empty($query) === FALSE)

{

if (FALSE === ($this->result = mysqli_query($this->link, $query)))

{

throw new Exception('Error performing query ' . $query . ' Error message :' .mysqli_error($this->link));

}

}

}

 

// fetch row from result set

public function fetch()

{

if (FALSE === ($row = mysqli_fetch_assoc($this->result)))

{

mysqli_free_result($this->result);

return FALSE;

}

return $row;

}

 

// get insertion ID

public function getInsertID()

{

return mysqli_insert_id($this->link);

}

// count rows in result set

public function countRows()

{

if ($this->result !== NULL)

{

return mysqli_num_rows($this->result);

}

}

// implement destructor to close the database connection

function __destruct()

{

mysqli_close($this->link);

}

}

As I said before, the brand new “factory()” method added to the above “MySQL” class is simply responsible for returning Singletons of the class in question. It has been declared static for obvious reasons, since it uses the static “$instance” property to reduce to one the number of class instances created within a given script.

As you can see, the implementation of this factory method is straightforward and easy to follow, and additionally ensures that, if the “User” class calls it inside its context, only one single instance of the database handler will be returned.

Even though this method is not a truly clean solution that solves the dependency-related issue that exists between both “User” and “MySQL” classes, it does help to implement their relationship in a more efficient way.

However, the next step that must be taken to complete this improvement process is to change the definition of the “User” class, so it can take advantage of the factory method discussed a moment ago.

This modification will be discussed in the last section of this tutorial, so to learn more about it, click on the link below and read the next few lines.



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