|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #67409 Clone arguments
Submitted: 2014-06-10 10:12 UTC Modified: 2017-10-05 11:01 UTC
From: flip101 at gmail dot com Assigned:
Status: Wont fix Package: Class/Object related
PHP Version: Irrelevant OS:
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.
Block user comment
Status: Assign to:
Bug Type:
From: flip101 at gmail dot com
New email:
PHP Version: OS:


 [2014-06-10 10:12 UTC] flip101 at gmail dot com
It would be nice if the clone function could take arguments. Some scenario's where this would be useful:
1. The type of cloning, like deep-cloning (see given example)
2. Initialize with external values, similar to a constructor

The solution to the second use case would be to clone first and then set the property right on the clone. Which would need a public property, a setter or a hydration method (like reflection). Something that is sometimes not desired.

Test script:
class Bar { }
class Foo {
	public function __construct() {
		$this->bar = new Bar;
	public function __clone($deep = false) {
		if ($deep) {
			$this->bar = clone $this->bar;
$a = new Foo;
$b = clone $a(true);


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2014-06-10 10:33 UTC] flip101 at gmail dot com
To clarify "The solution to the second use case" --> the situation right now without this feature.

in case __invoke() is implemented it is not called. Same behaviour as __construct(). See:
 [2014-06-11 03:36 UTC] phpmpan at mpan dot pl
`clone` creates a clone of an object. That is: an object equivalent to the original one. If you want to create a method with a different contract then... create a method with a different contract.
 [2017-10-05 11:01 UTC]
-Status: Open +Status: Wont fix
 [2017-10-05 11:01 UTC]
The `clone $a(true)` syntax is already in use: It will first call $a(true) and then clone the return value. As such we cannot introduce this functionality, at least in this form, without breaking existing code.

You can easily achieve the desired behavior with a separate method:

class Foo {
    public function deepClone() {
        $obj = clone $this;
        $obj->bar = clone $this->bar;
        return $obj;

This does *not* require the "bar" property to be public, because visibility is bound to a certain scope, not to a certain object.
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Wed Feb 08 02:05:49 2023 UTC