|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #55833 Dereferencing clone operation
Submitted: 2011-10-02 22:39 UTC Modified: 2014-10-12 14:59 UTC
Avg. Score:3.8 ± 0.7
Reproduced:5 of 5 (100.0%)
Same Version:2 (40.0%)
Same OS:1 (20.0%)
From: hakon dot trandal at gmail dot com Assigned: nikic (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2011-10-02 22:39 UTC] hakon dot trandal at gmail dot com
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()
$news = (clone $criteria)
$articles = (clone $critera)


This suggestion originated on stack overflow:

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

Actual result:
Parse error: syntax error, unexpected T_OBJECT_OPERATOR


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2014-10-12 14:59 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2014-10-12 14:59 UTC]
Supported in PHP 7.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Jan 24 10:01:24 2021 UTC