Friday, September 12, 2008

PHP Pattern

Singleton Pattern
The singleton pattern applies to those situations in which there needs to be a single instance of a class. Implementing this pattern makes this single instance easily accessible by many other objects.

Example


class Example
{
// Hold an instance of the class
private static $instance;
// A private constructor; prevents direct creation of object
private function __construct()
{
echo 'I am constructed';
}

// The singleton method
public static function singleton()
{
if (!isset(self::$instance)) {
$c = __CLASS__;
self::$instance = new $c;
}
return self::$instance;
}

// Example method
public function bark()
{
echo 'Woof!';
}

// Prevent users to clone the instance
public function __clone()
{

trigger_error('Clone is not allowed.', E_USER_ERROR);
}
}
?>

Magic Methods
The function names _construct, _sleep, _wakeup, _tostring are all magical methods in PHP. There cannot be any functions with these names in the classes. These functions can be had only if we want the magical functionality associated with them.

_sleep
The _sleep magic method is used to close any database connections that the object may have. It is also used to commit pending data and perform similar cleanup tasks. It is also used if there are very large objects which do not have to be saved completely.

_wakeup
The _wakeup magic method is used to reconstruct any resources that the object may have. The basic function of _wakeup is to reestablish any database connections that may have been lost during serialization and perform other initialization tasks.

Example of sleep and wakeup


class Conn {
protected $link;
private $server, $username, $password, $db;
public function __construct($server, $username, $password, $db)
{
$this->server = $server;
$this->username = $username;
$this->password = $password;
$this->db = $db;
$this->connect();
}

private function connect()
{
$this->link = mysql_connect($this->server, $this->username, $this->password);
mysql_select_db($this->db, $this->link);
}

public function __sleep()
{
mysql_close($this->link);
}

tostring
The _tostring magic method allows a class to decide that how it will react when it is converted to string.

Example


// Declare a simple class
class Test
{
public $foo;
public function __construct($foo) {
$this->foo = $foo;
}

public function __toString() {
return $this->foo;
}
}

$class = new Test('Hello');
echo $class;
?>

Cloning
An object copy is created by using the clone keyword. An object's __clone() method cannot be called directly. When an object is cloned, PHP will perform a shallow copy of all of the object's properties. Any properties that are references to other variables, will remain references. If a __clone() method is defined, then the newly created object's __clone() method will be called, to allow any necessary properties that need to be changed.

Example


class SubObject
{
static $instances = 0;
public $instance;
public function __construct() {
$this->instance = ++self::$instances;
}

public function __clone() {
$this->instance = ++self::$instances;
}
}

class MyCloneable
{
public $object1;
public $object2;
function __clone()
{

// Force a copy of this->object, otherwise
// it will point to same object.
$this->object1 = clone($this->object1);
}
}

$obj = new MyCloneable();
$obj->object1 = new SubObject();
$obj->object2 = new SubObject();
$obj2 = clone $obj;
print("Original Object:\n");
print_r($obj);
print("Cloned Object:\n");
print_r($obj2);
?>

No comments: