Req #70414 [Opn->Wfx]: FR: support to clone() keyword
| From: | nikic@php.net | Date: | Sun, 27 Mar 2016 16:23:12 +0000 |
| Subject: | Req #70414 [Opn->Wfx]: FR: support to clone() keyword | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-200164@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: nikic@php.net
Reported by: david dot proweb at gmail dot com
Summary: FR: support to clone() keyword
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
Package: Scripting Engine problem
PHP Version: Next Minor Version
Block user comment: N
Private report: N
New Comment:
Closing as Won't Fix, because:
a) In PHP 7 you can write (clone $user)->find(1), covering your first point.
b) As requinix points out, the calling code has no business touching private properties of the
object. If you want to do something, you are free to use one of the usual hacks for avoiding
visibility checks (e.g. reflection or closure rebinding).
Previous Comments:
------------------------------------------------------------------------
[2015-09-02 21:37:40] requinix@php.net
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.
------------------------------------------------------------------------
[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