note 88210 added to language.oop5.cloning
| From: | semtexatigdotcomdotbr at osu1 dot php dot net | Date: | Thu, 15 Jan 2009 13:53:37 +0000 |
| Subject: | note 88210 added to language.oop5.cloning | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-149256@lists.php.net to get a copy of this message | ||
If you create a singleton class to implement a database connection with PDO and try to clone your
object, PHP will allow it.
i.e.:
class Database {
protected static $instance = null;
private function __construct() { }
public function __clone() { }
public static function getInstance() {
if(self::$instance == null) {
try {
self::$instance = new PDO('dsn', 'user', 'pass');
} catch(Exception $e) { }
}
return self::$instance;
}
}
$db = Database::getInstance();
$db2 = clone $db;
This shouldn't work, but it does... it creates a new instance of the object.
----
Server IP: 69.147.83.197
Probable Submitter: 201.7.117.200
----
Manual Page -- http://www.php.net/manual/en/language.oop5.cloning.php
Edit -- https://master.php.net/note/edit/88210
Del: integrated -- https://master.php.net/note/delete/88210/integrated
Del: useless -- https://master.php.net/note/delete/88210/useless
Del: bad code -- https://master.php.net/note/delete/88210/bad+code
Del: spam -- https://master.php.net/note/delete/88210/spam
Del: non-english -- https://master.php.net/note/delete/88210/non-english
Del: in docs -- https://master.php.net/note/delete/88210/in+docs
Del: other reasons-- https://master.php.net/note/delete/88210
Reject -- https://master.php.net/note/reject/88210
Search -- https://master.php.net/manage/user-notes.php