Bug #74862 [Com]: Unable to clone instance when private __clone defined
| From: | spam2 at rhsoft dot net | Date: | Thu, 06 Jul 2017 06:36:25 +0000 |
| Subject: | Bug #74862 [Com]: Unable to clone instance when private __clone defined | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-209838@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=74862&edit=1
ID: 74862
Comment by: spam2 at rhsoft dot net
Reported by: daniel at ciokix dot ro
Summary: Unable to clone instance when private __clone
defined
Status: Open
Type: Bug
Package: Scripting Engine problem
Operating System: Ubuntu 14.04
PHP Version: 5.6.30
Block user comment: N
Private report: N
New Comment:
the only probably bug here is that you don't get a error message that you can't set
arbitrary magic methods as private like you get it for __construct() and __get() but given that PHP5
is EOL I won't expect that to change
Previous Comments:
------------------------------------------------------------------------
[2017-07-06 04:23:13] daniel at ciokix dot ro
Description:
------------
When defining the __clone magic method as private, the instance cannot be cloned from within the
method implementing parent class.
Test script:
---------------
class a {
private function __clone()
{
}
private function __construct()
{
}
public static function getInstance()
{
return new static();
}
public function cloneIt()
{
$a = clone $this;
return $a;
}
}
class c extends a {
}
// private constructor
$d = c::getInstance();
// private clone
$e = $d->cloneIt();
Link to 3v4l: https://3v4l.org/bWr72
Expected result:
----------------
Expecting the same behavior as for __construct: a successfully cloned object.
Actual result:
--------------
The following misleading fatal error is thrown:
Fatal error: Uncaught Error: Call to private c::__clone() from context 'a'
which is not the case as clone is called from a context and not c.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=74862&edit=1