Req #67409 [Opn->Wfx]: Clone arguments

From: Date: Thu, 05 Oct 2017 11:01:40 +0000
Subject: Req #67409 [Opn->Wfx]: Clone arguments
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-211526@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=67409&edit=1 ID: 67409 Updated by: nikic@php.net Reported by: flip101 at gmail dot com Summary: Clone arguments -Status: Open +Status: Wont fix Type: Feature/Change Request Package: Class/Object related PHP Version: Irrelevant Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2014-06-11 03:36:49] 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. ------------------------------------------------------------------------ [2014-06-10 10:33:01] 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: http://codepad.viper-7.com/eBL3WY ------------------------------------------------------------------------ [2014-06-10 10:12:44] flip101 at gmail dot com Description: ------------ 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: --------------- <?php 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); ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=67409&edit=1

« previous php.bugs (#211526) next »