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

Explain the difference between $message and $$message?

560


Can php replace javascript?

536


What is difference between php and wordpress?

507


What is orm in php framework?

502


What is the difference between null and empty?

585






i m a B.E,passed out few yrs bak failed to find good job due to less marks now thinkin of a career as PHP developer. is it a lucarative field and wat r career prospects; hv heard dat initial salary is very less bout 6k also most jobs r contrct jobs.pls guide me need ur valuable advice.pls hellppp!! thnx..

1679


So if md5() generates the most secure hash, why would you ever use the less secure crc32() and sha1()?

559


What types of MYSQL function available for affecting columns

555


Is php deprecated?

538


Is php dying 2018?

550


What is self in php?

558


What is isset php?

525


What are php filters?

522


Suppose we receive a form submitted by a post to subscribe to a newsletter. This form has only one field, an input text field named email. How would we validate whether the field is empty? Print a message "the email cannot be empty" in this case?

687


What is design pattern? Explain all including singleton pattern?

543