Req #70414 [Opn]: FR: support to clone() keyword

From: Date: Wed, 02 Sep 2015 21:37:40 +0000
Subject: Req #70414 [Opn]: FR: support to clone() keyword
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-195720@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70414&edit=1 ID: 70414 Updated by: requinix@php.net Reported by: david dot proweb at gmail dot com Summary: FR: support to clone() keyword Status: Open Type: Feature/Change Request Package: Scripting Engine problem PHP Version: Next Minor Version Block user comment: N Private report: N New Comment: Calling code should not even be aware of the existence of private or protected variables, let alone need to modify them after cloning. Like the other request, your use case is easily solved with a userland function. Previous Comments: ------------------------------------------------------------------------ [2015-09-02 20:57:56] david dot proweb at gmail dot com Description: ------------ It should works exactly like the clone, but with some features. // Usage. clone($user)->find(1); // Currently (too verbose): $userClone = clone $user; $userClone = $userClone->find(1); // __clone() reinforcement: callback that can // - get/set private/protected attributes or // call private/protected methods. clone($user, function ($baseObject) use ($externalData) { $this->privateAttribute = $baseObject->privateAttribute + $externalData; }); Signature (pseudo-code): function clone ($object, callable $callback = null) { assert(is_object($object)); if (!$callback) return clone $object; // __clone() is called here. $clone = clone $object; // Pass $clone as $this, and $object as first parameter. // Before call(), __clone() is called. $callback->call($clone, $object); return $clone; } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=70414&edit=1

« previous php.bugs (#195720) next »