php.net |  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: 2012-03-29 13:48 UTC
Votes:2
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:
Status: Open Package: Documentation problem
PHP Version: 5.3.10 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: antickon at gmail dot com
New email:
PHP Version: OS:

 

 [2012-03-26 14:33 UTC] antickon at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/language.operators.precedence
---
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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-03-29 13:37 UTC] nikic@php.net
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 
preferable.
 [2018-01-10 10:32 UTC] pasindu@php.net
So is the suggestions here to remove "[" from the docs or maybe add note about this?
 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Wed Nov 21 20:01:26 2018 UTC