Design a class that should always return a single object ?

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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is active record in php?

479


What is form action php?

583


Is apache needed for php?

502


What does the function get_magic_quotes_gpc() means?

501


What is namespace and use in php?

522






How many types of array are there in php?

570


Explain php parameterized functions.

553


What are objects in php?

522


How to join multiple strings stored in an array into a single string?

510


Tell me how to execute an sql query? How to fetch its result?

491


Why do we use inheritance in php?

526


Explain about a search-friendly site looks like?

486


Tell us what does $_cookie means?

569


What is difference between mysql_fetch_array and mysql_fetch_assoc?

495


What is repository in php?

524