Req #55833 [Opn->Csd]: Dereferencing clone operation
| From: | nikic@php.net | Date: | Sun, 12 Oct 2014 14:59:22 +0000 |
| Subject: | Req #55833 [Opn->Csd]: Dereferencing clone operation | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-188037@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=55833&edit=1
ID: 55833
Updated by: nikic@php.net
Reported by: hakon dot trandal at gmail dot com
Summary: Dereferencing clone operation
-Status: Open
+Status: Closed
Type: Feature/Change Request
Package: Scripting Engine problem
PHP Version: Irrelevant
-Assigned To:
+Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
Supported in PHP 7.
Previous Comments:
------------------------------------------------------------------------
[2011-10-02 22:39:38] hakon dot trandal at gmail dot com
Description:
------------
This suggestion is similar to "Instance and method call/property access"[1] and array
dereferencing[2].
Currently, if you want to perform an operation on a cloned object you have to assign the result to a
temporary variable:
$clonedObject = clone $otherObject;
$result = $clonedObject->doSomething();
It should be possible to call "doSomething()" directly after the clone operation:
$result = (clone $otherObject)->doSomething();
This would be especially useful in fluent interfaces:
$criteria = ArticleQuery::create()
->filterByIsPublished(true)
->orderByPublishFrom(Criteria::DESC)
->joinWith('Article.Author')
->limit(10);
$news = (clone $criteria)
->filterByType('news')
->find();
$articles = (clone $critera)
->filterByType('article')
->find();
[1] https://wiki.php.net/rfc/instance-method-call
[2] http://schlueters.de/blog/archives/138-Features-in-PHP-trunk-Array-dereferencing.html
This suggestion originated on stack overflow:
http://stackoverflow.com/questions/7629196/why-must-we-assign-a-clone-to-a-new-variable/
Test script:
---------------
class MyClass {
public $myVar = "original";
public function doSomething() {
$this->myVar = "did something";
return $this;
}
}
$a = new MyClass();
echo (clone $a)->doSomething()->myVar;
echo $a->myVar;
Expected result:
----------------
did something
original
Actual result:
--------------
Parse error: syntax error, unexpected T_OBJECT_OPERATOR
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=55833&edit=1