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

Removing the factory method from the MySQL class - 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

Remember what I said in the previous section? Due to the proper application of the Dependency Injection pattern, it’s possible to remove the factory method defined inside the earlier MySQL abstraction class, since in this case, it will be pretty useless for creating persistent objects.

However, the best approach to understanding why removing this factory method won’t cause any impact in the way that those persistent objects are spawned is by examining some functional code. So, below is the modified definition of the MySQL handling class, this time without exposing its factory method. Here it is:

class MySQL

{

private $result = NULL;

private $link = NULL;

 

// 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);

}

}

There you have it. As I explained a moment ago, now the “MySQL” class lacks its factory method, but its functionality remains practically the same. So, what’s the point in removing it, after all? Good question, indeed.

In reality there’s no an actual need to get rid of this method at all. Nonetheless, I wanted to do this so you could see that even without its presence, it’s feasible to build the same persistent objects that you saw before with only minor hassles.

But guess what? Showing you the example will be the perfect conclusion for this last part of the series. So click on the link below and read the next few lines. We’re almost finished!



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