php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74862 Unable to clone instance when private __clone defined
Submitted: 2017-07-06 04:23 UTC Modified: -
From: daniel at ciokix dot ro Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: 5.6.30 OS: Ubuntu 14.04
Private report: No CVE-ID:
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: daniel at ciokix dot ro
New email:
PHP Version: OS:

 

 [2017-07-06 04:23 UTC] 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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-07-06 06:36 UTC] spam2 at rhsoft dot net
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
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Tue Aug 29 15:01:52 2017 UTC