Main Menu




browser lang:en

Users Area

Client Login

today cache size is:82392

 

Holyguard rss
rss 2.0 for all sections

 

Portfolio

Some of my projects:
BluPool
L'Pratone
Travel in Hotel
Yacht Elements
AbruzzoWeb
Il Mastino
CSI Teramo
Innovazione S.p.a.




Comments

attached files not working !
05/11/2009 by Adnan
Tuvok
14/02/2009 by
Nice script
07/02/2009 by desaj
Other solutions
07/02/2009 by Mike
well
06/02/2009 by holy
Theif
06/02/2009 by
really good
02/11/2008 by

Read More »


RAPIDQ

Rapid-Q:The lost files
My old library where i have collected all the rapidq scripts and italian help


Today my pagerank is: Free Page Rank Tool

PHP - The Singleton Pattern

The Singleton Pattern is one of the GoF (Gang of Four) Patterns. This particular pattern provides a method for limiting the number of instances of an object to just one. It's an easy pattern to grasp once you get past the strange syntax used.

Consider the following class:

PHP Code:
class Database { 
    
public function __construct() { ... }     
public function connect() { ... }    
public function query() { ... }  
    
...     
} 
This class creates a connection to our database. Any time we need a connection we create an instance of the class, such as:

PHP Code:
$pDatabase = new Database(); 
$aResult = $pDatabase->query('...');  
Lets say we use the above method many times during a script's lifetime, each time we create an instance we're creating a new Database object (we're also creating a new database connection, but that's irrelevant in this example) and thus using more memory. Sometimes you may intentionally want to have multiple instances of a class but in this case we don't.

The Singleton method is a solution to this common problem. To make the Database class a Singleton we first need to add a new property to the class, we'll call this $m_pInstance:

PHP Code:
class Database { 
    
// Store the single instance of Database     

private static $m_pInstance;     
...         
}
As the comment states, this property will be used to store the single instance of our Database class. You should also note that this property must be static.

Next we need to change the constructor's scope to private. This is one of the strange syntaxes that usually confuse people.

PHP Code:
class Database{
    
// Store the single instance of Database
    
private static $m_pInstance;

private function __construct() { ... }
}
By making the constructor private we have prohibited objects of the class from being instantiated from outside the class. So for example the following no longer works outside the class:

PHP Code:
$pDatabase = new Database();
  
We now need to add a method for creating and returning our Singleton. Add the following method to the Database class:

PHP Code:
public static function getInstance(){
    
    if (!self::$m_pInstance){
        self::$m_pInstance = new Database();
    
    } 
    
return self::$m_pInstance;
}  
This funny looking function is responsible for handling our object instance. It's relatively easy to understand, basically we check our static property $m_pInstance, if it is not valid we create a new instance of the Database class by calling the constructor. Remember, we made the __construct() method private, so an instance of the object can only be created from within the class' methods. Finally the function returns a reference to our static property. On subsequent calls to getInstance(), $m_pInstance will be valid and thus the reference will be returned - no new instances are created.

So our Database class now looks something like this

PHP Code:
class Database{

// Store the single instance of Database
    
private static $m_pInstance;     
    
private function __construct() { ... }
    
public static function getInstance(){ 
        
if (!self::$m_pInstance){   
            
self::$m_pInstance = new Database();     
        
}     
  
return self::$m_pInstance;     
    
}     
}
You can now get an instance of the Database class from anywhere (without using globals or function arguments) in your project. Here's an example and comparison:

This is the usual way we create objects:
PHP Code:
    $pDatabase = new Database(); 
    $aResult = $pDatabase->query('...'); 
    
  
This is the Singleton way:
PHP Code:
    $pDatabase = Database::getInstance();
    $aResult = $pDatabase->query('...'); 
  
To conclude, the Singleton is an easy-to-use design pattern for limiting the number of instances of an object.

Rating:
0.0
0 votes
1 2 3 4 5

Comments

Insert your comment

Titolo
Messaggio
Nome Utente
e-mail (se vuoi ricevere le risposte a questo post anche via mail)

Videos


Contents

Complete guide for Jquery Developers

27/02/2010 

Have you ever had to develop something yourself only to find out that there had already been…

in:JQuery (0 comments)

How can i insert HTML code in my posts?

14/02/2010 

The situation begins with your blog or website and you need to post some code on a particular…

in:The Holy Faq's (0 comments)

12 undocumented tricks for Google Buzz

14/02/2010 

So. Google just recently announced Google Buzz. I’m not sure about you, but I…

in:Blog (0 comments)

Display an image from a MySQL database in a web page via PHP

14/02/2010 

There's lots of clever scripts around to tell you how to get images in and out of…

in:PHP scripts (0 comments)

MySQL - Find Locations Nearest Known Coordinates

03/02/2010 

Spherical Law of Cosines Suppose that we want to find the five nearest places to (47.470779, -87.890699) using Spherical…

in:MySql (0 comments)

The 7 free best CSS editors

01/02/2010 

I have always used Dreamweaver, and love it. But, I have been thinking, what are the FREE CSS…

in:Free Software (0 comments)

Upgrade to Alsa 1.0.20 on Ubuntu Jaunty 9.04

09/01/2010 

Advanced Linux Sound Architecture (known by the acronym ALSA) is a Linux kernel component intended to replace the…

in:Blog (0 comments)

PHP - The Singleton Pattern

26/11/2009 

The Singleton Pattern is one of the GoF (Gang of Four) Patterns. This particular pattern provides a…

in:Scripts and tutorials (0 comments)

Read more »


Tag Clouds


Complete guide Jquery DevelopersHow insert HTML code posts12 undocumented tricks Google BuzzDisplay image from MySQL database page PHPMySQL Find Locations Nearest Known CoordinatesThe free best editorsUpgrade Alsa 1020 Ubuntu Jaunty


Add to Technorati Favorites