Design a class that should always return a single object ?

Answers were Sorted based on User's Feedback



Design a class that should always return a single object ?..

Answer / 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

Design a class that should always return a single object ?..

Answer / amitverma

class DB {
private static $_singleton;
private $_connection;

private function __construct(){
$this->_connection = mysql_connect();
}

public static function getInstance(){
if (is_null (self::$_singleton)) {
self::$_singleton = new DB();
}
return self::$_singleton;
}
}

$db = DB::getInstance();

Is This Answer Correct ?    2 Yes 2 No

Design a class that should always return a single object ?..

Answer / sam

In my opinion class can not return any object. Only methods
can return the objects

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More PHP Interview Questions

How to copy a file?

0 Answers  


How can I find the width and height of an image resource?

2 Answers   Rushmore Consultancy,


How is it possible to parse a configuration file?

0 Answers  


What is php ci?

0 Answers  


What are the different types of errors in PHP?

5 Answers  






How to compare two strings with comparison operators in php?

0 Answers  


Is php dead 2019?

0 Answers  


What is the difference between characters 23 and x23?

0 Answers  


class Database { public static $_instance; public static function getInstance() { if(!isset(self::$_instance)) self::$_instance = new Database(DB_SERVER, DB_USER, DB_PASS, DB_NAME); print_r(self::$_instance); return self::$_instance; } } can any one explain "self::$_instance = new Database(DB_SERVER, DB_USER, DB_PASS, DB_NAME);" this line

1 Answers  


Why does php start with variables?

0 Answers  


can any one find and tell the difference between dot net and php which one is best ? which one we get more salary? which one is stable and which one is best for freshers and also better in future and carrer ? which one we wil get more salary sir ? please send ur valuable suggestions to kiranpulsar2007@gmail.com

3 Answers  


armstrong number by using php while number is given by the keyboard.?

0 Answers  


Categories