note 88210 deleted from language.oop5.cloning by danbrown

From: Date: Thu, 15 Jan 2009 16:50:28 +0000
Subject: note 88210 deleted from language.oop5.cloning by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-149283@lists.php.net to get a copy of this message
Note Submitter: semtex at ig dot com dot br ---- 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.

« previous php.notes (#149283) next »