<?php
// sample code to illustrate the use of singleton design pattern
// http://royads.blogspot.com
class mysqlClass
{
protected static $_instance;
public $username;
function __construct()
{
}
public static function getInstance()
{
if(null === self::$_instance)
{
self::$_instance = new mysqlClass();
}
return self::$_instance;
}
}
$singleDbConnection = mysqlClass::getInstance();
$singleDbConnection->username = 'user1';
$isItOtherDbConnection = mysqlClass::getInstance();
print $isItOtherDbConnection->username;
// c the output of above line it prints the value which is set by the first object. Now this is what is singleton.
?>