|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2005-06-22 17:44 UTC] j dot amel83 at gmail dot com
Description:
------------
If there is protected/private constructor method, there is no way to clone the class, it?s seems that the clone method try to create new object from the class using constructor and then set it?s properties like the cloned one.
Reproduce code:
---------------
<?php
class Test {
protected $name;
private function __construct() {
$this->name = 'ali';
}
public function __clone() {
throw new Exception('Clone not supported Exception');
}
}
$t1 = new Test();
$t2 = clone $t;
?>
Expected result:
----------------
Fatal error: Uncaught exception 'Exception' with message 'Clone not Supported Exception' in D:\WebServerRoot\FrameWork\test.php:15 Stack trace: #0
Actual result:
--------------
Fatal error: Call to private Test::__construct() from context '' in D:\WebServerRoot\FrameWork\test.php on line 15
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Dec 13 23:00:01 2025 UTC |
oh, i wrote bad code for this bug see this one: i want to have max one instance of Session class an i found ever things right, thanks. <?php class Session { protected static $instance = null; public static function getSession() { if(self::$instance == null) { $class = __CLASS__; self::$instance = new $class(); } return self::$instance; } private function __construct() { /* * * */ } public function __clone() { throw new Exception('Clone not supported Exception'); } } $s1 = Session::getSession(); $s2 = clone $s1; ?>