php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #35513 A method of grouping variables so that the order of operators can be made clear
Submitted: 2005-12-02 08:28 UTC Modified: 2005-12-02 14:58 UTC
From: david at tulloh dot id dot au Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 6CVS-2005-12-02 (CVS) OS:
Private report: No CVE-ID: None
 [2005-12-02 08:28 UTC] david at tulloh dot id dot au
Description:
------------
When using objects it can become very ambiguous what is occuring once you start using variable variable names.  The reproduce code shows a non-useful example of where this might occur.

A concise example of the problem:
$ob->field[0]  ==> array[0] ==> item
$ob->$field[0] ==> $ob->'b' ==> array

Being able to use brackets or something similar to force the -> operator to act before the [] would allow for a choice of functionality in the second example and provide the ability to make the code clearer.

$ob->($field[0]) 
($ob->$field)[0]

Both of these examples are currently not possible, a syntax error, unexpected ( or [ occurs.

Reproduce code:
---------------
<?php
class Arrayise {
    function __get($name) {
        return array($name);
    }
}

$ob = new Arrayise;
$field = 'bar';
var_dump($ob->$field[0]); // b
var_dump($ob->field[0]);  // bar
?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-12-02 14:58 UTC] tony2001@php.net
Use {}.
var_dump($ob->{$field}[0]); // bar
var_dump($ob->{$field[0]}); // array(0=>"b")

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 17:01:29 2024 UTC