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
What are the different errors in php?
Write a program to get lcm of two numbers using php?
What's the difference between accessing a class method via -> and via ::?
What is a helper function?
How to download files from an external server with code in php?
How to insert an new array element in array?
Where php language is used?
Can a trait extend a class php?
Under what circumstance is it impossible to assign a default value to a parameter while declaring a function?
Do you know how can php and html interact?
What is session and why do we use it?
Php says that an array is an ordered map. But how the values are ordered in an array?
How many keywords are there in php?
What is active record in php?
What are the three classes of errors that can occur in php?