Home arrow PHP arrow Page 2 - Decoupling the Interaction Between Objects with Factory Methods

Review: implementing a simple factory method in PHP 5 - PHP

Welcome to the fifth part of a six-part series on implementing factory methods in PHP 5. In this part, I show how the removal of a factory method inside the constructor of the class that originates persistent objects can produce a huge impact in the way that those objects handle its dependency, which happens to be a database handler object.

TABLE OF CONTENTS:
  1. Decoupling the Interaction Between Objects with Factory Methods
  2. Review: implementing a simple factory method in PHP 5
  3. Using a factory method more efficiently
  4. Putting the sample classes to work
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
December 14, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In the introduction, I said that a simple factory method can improve the relationship established between a persistent class and its corresponding dependency, which in this case happens to be a MySQL database handler. However, it’d be quite useful to recall how these sample classes were defined in the preceding tutorial, and how they can be put to work together as well.

Bearing this in mind, below I listed the signatures of those classes, accompanied by a short script that shows how to use them in a concrete situation.

First, take a look at the definition of the MySQL handling class:

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

}

}

And now that you've surely recalled how the “MySQL” class looked, here’s the other one, which takes advantage of the functionality of “MySQL” to save and retrieve its properties from a specified database table:

class User

{

private $data = array();

private $id = NULL;

private $db = NULL;

 

// constructor

public function __construct($id = NULL)

{

$this->db = MySQL::factory('host', 'user', 'password', 'database');

if ($id !== NULL)

 {

$this->id = $id;

$this->db->query('SELECT * FROM users WHERE id=' . $this->id);

$this->data = $this->db->fetch();

}

}

 

// set undeclared property

public function __set($property, $value)

{

if ($property !== 'name' and $property !== 'email')

{

return;

}

$this->data[$property] = $value;

}

 

// get undeclared property

public function __get($property)

{

if (isset($this->data[$property]) === TRUE)

{

return $this->data[$property];

}

}

 

// save object to session variable

public function __destruct()

{

if ($this->id === NULL)

{

$this->db->query("INSERT INTO users (id, name, email) VALUES (NULL, '$this->name', '$this->email')");

}

else

{

$this->db->query("UPDATE users SET name = '$this->name', email = '$this->email' WHERE id = $this->id");

 }

}

}

As I explained before, this “User” class is a blueprint for building persistent objects that use a MySQL table to store the values assigned to its pertinent properties. However, the most relevant detail concerning the way this class works is the implementation of its constructor, since this method is entirely responsible for building an instance of the database handler. Even though the factory method of the database handler returns only Singletons to client code, this obviously isn’t a good thing. The persistent class should only be tasked with handling its properties, not with spawning other objects.

In addition, here’s a basic example that shows how to make use of the two classes shown previously for creating two persistent user objects:

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

Well, to be frank, the above script looks pretty clean, and works well enough, since the persistent objects are constructed in a straightforward fashion. Nonetheless, things are a bit more complicated behind the scenes due to the incorrect instantiation of the database handler.

So, can this issue be addressed easily? Fortunately, the answer is a resounding yes! A small change in the definition of the constructor of the persistent “User” class will solve this problem.

But the full details of this modification will be discussed in the next section. So, go ahead and read the following segment.



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