Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

Design a class that should always return a single object ?

Answer Posted / webguy

The answer is to use : The singleton pattern

Description:

Some application resources are exclusive in that there is
one and only one of this type of resource. For example, the
connection to a database through the database handle is
exclusive. You want to share the database handle in an
application because it's an overhead to keep opening and
closing connections, particularly during a single page fetch.

The singleton pattern covers this need. An object is a
singleton if the application can include one and only one of
that object at a time. The code in Listing 3 shows a
database connection singleton in PHP V5.

Ex: Singleton.php


<?php
require_once("DB.php");

class DatabaseConnection
{
public static function get()
{
static $db = null;
if ( $db == null )
$db = new DatabaseConnection();
return $db;
}

private $_handle = null;

private function __construct()
{
$dsn = 'mysql://root:password@localhost/photos';
$this->_handle =& DB::Connect( $dsn, array() );
}

public function handle()
{
return $this->_handle;
}
}

print( "Handle = ".DatabaseConnection::get()->handle()."\n" );
print( "Handle = ".DatabaseConnection::get()->handle()."\n" );
?>


This code shows a single class called DatabaseConnection.
You can't create your own DatabaseConnection because the
constructor is private. But you can get the one and only one
DatabaseConnection object using the static get method. The
UML for this code is shown in Figure 3.

Figure 3. The database connection singleton
The database connection singleton

The proof in the pudding is that the database handle
returned by the handle method is the same between two calls.
You can see this by running the code on the command line.

% php singleton.php
Handle = Object id #3
Handle = Object id #3
%


The two handles returned are the same object. If you use the
database connection singleton across the application, you
reuse the same handle everywhere.

You could use a global variable to store the database
handle, but that approach only works for small applications.
In larger applications, avoid globals, and go with objects
and methods to get access to resources.

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is split function in php?

916


What is the use of header() function in PHP? What the Limitation of HEADER()?

986


Tell me how stop the execution of a php scrip?

930


Explain how can we increase the execution time of a php script?

938


Do you know what is the difference between mysql_fetch_object() and mysql_fetch_array()?

932


What is query string php?

923


What are the uses of explode() function?

994


How many functions are there in php?

937


What is a http session?

905


How does session work in php?

947


What is meant by ‘passing the variable by value and reference' in php?

935


Explain object-oriented methodology in php?

928


What does odbc do in context with php?

972


What are php errors?

899


Which php function will attach one file to another?

930