php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33436 clone problem
Submitted: 2005-06-22 17:44 UTC Modified: 2005-06-23 10:43 UTC
From: j dot amel83 at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.0.4 OS: Win XP, Apache 2.0.54
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: j dot amel83 at gmail dot com
New email:
PHP Version: OS:

 

 [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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-06-22 19:42 UTC] tony2001@php.net
Remove the last line (the one with "clone") and you'll see the very same result.
Clone has nothing to do with it - you're trying to instantiate class with private constructor.
 [2005-06-23 08:50 UTC] j dot amel83 at gmail dot com
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;
?>
 [2005-06-23 10:43 UTC] tony2001@php.net
.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jun 29 10:01:29 2024 UTC