From:
Operating system:
PHP version: Irrelevant
Package: Scripting Engine problem
Bug Type: Feature/Change Request
Bug description:Dereferencing clone operation
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 bug report at https://bugs.php.net/bug.php?id=55833&edit=1
--
Try a snapshot (PHP 5.4):
https://bugs.php.net/fix.php?id=55833&r=trysnapshot54
Try a snapshot (PHP 5.3):
https://bugs.php.net/fix.php?id=55833&r=trysnapshot53
Try a snapshot (trunk):
https://bugs.php.net/fix.php?id=55833&r=trysnapshottrunk
Fixed in SVN:
https://bugs.php.net/fix.php?id=55833&r=fixed
Fixed in SVN and need be documented:
https://bugs.php.net/fix.php?id=55833&r=needdocs
Fixed in release:
https://bugs.php.net/fix.php?id=55833&r=alreadyfixed
Need backtrace:
https://bugs.php.net/fix.php?id=55833&r=needtrace
Need Reproduce Script:
https://bugs.php.net/fix.php?id=55833&r=needscript
Try newer version:
https://bugs.php.net/fix.php?id=55833&r=oldversion
Not developer issue:
https://bugs.php.net/fix.php?id=55833&r=support
Expected behavior:
https://bugs.php.net/fix.php?id=55833&r=notwrong
Not enough info:
https://bugs.php.net/fix.php?id=55833&r=notenoughinfo
Submitted twice:
https://bugs.php.net/fix.php?id=55833&r=submittedtwice
register_globals:
https://bugs.php.net/fix.php?id=55833&r=globals
PHP 4 support discontinued:
https://bugs.php.net/fix.php?id=55833&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=55833&r=dst
IIS Stability:
https://bugs.php.net/fix.php?id=55833&r=isapi
Install GNU Sed:
https://bugs.php.net/fix.php?id=55833&r=gnused
Floating point limitations:
https://bugs.php.net/fix.php?id=55833&r=float
No Zend Extensions:
https://bugs.php.net/fix.php?id=55833&r=nozend
MySQL Configuration Error:
https://bugs.php.net/fix.php?id=55833&r=mysqlcfg