|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #61513 clone operator precedence is wrong
Submitted: 2012-03-26 14:33 UTC Modified: 2020-08-14 08:57 UTC
Avg. Score:2.0 ± 1.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: antickon at gmail dot com Assigned: cmb (profile)
Status: Closed Package: Documentation problem
PHP Version: 5.3.10 OS:
Private report: No CVE-ID: None
 [2012-03-26 14:33 UTC] antickon at gmail dot com
From manual page:
According to the Operator Precedence table, the clone operator has higher 
precedence than [.

This would mean clone $arraylike[0] is evaluated as ( clone $arraylike )[0], but 
it seems that the implementation does clone ( $arraylike[0] ). I think this is 
probably correct behavior, so I conclude the documentation is incorrect.

Test script:
$a = array( new stdClass() );
clone $a[0];

Expected result:
in this case, some type error at runtime since you can't clone an array

Actual result:
the object at offset 0 is cloned


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-03-29 13:37 UTC]
clone/new are defined with higher precedence in the parser. But there is no general `expr [ dim_offset ]` rule (only specific rules for various variable syntaxes), so it can't be interpreted as `(clone $a)[0]`.

Hard to say what one can do about this in the docs. Maybe removing [ from the list might be a viable option. Variable offset access is kinda outside the normal precedence rules.
 [2012-03-29 13:48 UTC] antickon at gmail dot com
Even if [ is an amalgam of syntactic constructs, precedence and associativity are 
properties it appears to have. Having it correctly documented is absolutely 
 [2018-01-10 10:32 UTC]
So is the suggestions here to remove "[" from the docs or maybe add note about this?
 [2020-08-14 08:57 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sat Dec 05 15:01:23 2020 UTC