php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #60408 Array/String element access on instantiation (same like class member in 5.4RC)
Submitted: 2011-11-29 12:06 UTC Modified: 2012-04-17 02:11 UTC
Votes:5
Avg. Score:4.8 ± 0.4
Reproduced:5 of 5 (100.0%)
Same Version:3 (60.0%)
Same OS:4 (80.0%)
From: bugzilla33 at gmail dot com Assigned: laruence (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.4.0RC2 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: bugzilla33 at gmail dot com
New email:
PHP Version: OS:

 

 [2011-11-29 12:06 UTC] bugzilla33 at gmail dot com
Description:
------------
Please add array constructor dereferencing support

like JavaScript, C# and all languages excluding PHP 5.4

Test script:
---------------
<?php

 $i=0;
 print(array('pa','th')[$i++]); // pa
 print(['wa','ss'][$i++]); // ss

?>

Expected result:
----------------
Parse error: syntax error, unexpected '[' in C:\htdocs\test.pl\array.php on line 4

Actual result:
--------------
pass

Patches

const_dereference_002.phpt (last revision 2011-11-30 09:46 UTC by laruence@php.net)
const_dereference_003.phpt (last revision 2011-11-30 09:46 UTC by laruence@php.net)
const_dereference_001.phpt (last revision 2011-11-30 09:46 UTC by laruence@php.net)
req60408.patch (last revision 2011-11-30 09:45 UTC by laruence@php.net)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-11-29 12:08 UTC] felipe@php.net
-Type: Bug +Type: Feature/Change Request
 [2011-11-29 14:40 UTC] bugzilla33 at gmail dot com
20 Jun 2011, PHP 5.4.0 Alpha 1 -> Added array dereferencing support. (Felipe)
11 Nov 2011, PHP 5.4.0 RC1 -> Added class member access on instantiation (e.g. (new foo)->bar()) support. (Felipe)

Many thanks for Felipe.
It would be nice to add: Array element access on instantiation
array()[$x] and [][$x]

Then everything would be fully useful, but not in part.
 [2011-11-29 14:42 UTC] bugzilla33 at gmail dot com
-Summary: Array constructor dereferencing support +Summary: Array element access on instantiation (same like class member in 5.4RC)
 [2011-11-29 14:42 UTC] bugzilla33 at gmail dot com
(edit summary)
 [2011-11-30 07:29 UTC] bugzilla33 at gmail dot com
>> echo array(1, 2, 3)[0]; //output 1
>> echo "foobar"[2]; //output o
>> echo "foobar"["foo"][0] // output f
>> works.

Excellent! Have you tested it with short array constructor?
echo [1, 2, 3][0]; //output 1


Are there any opportunities to make it public in the final PHP 5.4?
 [2011-11-30 07:36 UTC] laruence@php.net
support (of course).

yes, but it need to be disscussd first ;)
 [2011-11-30 07:47 UTC] bugzilla33 at gmail dot com
-Summary: Array element access on instantiation (same like class member in 5.4RC) +Summary: Array/String element access on instantiation (same like class member in 5.4RC)
 [2011-11-30 07:47 UTC] bugzilla33 at gmail dot com
/Summary change/ Array -> Array/String
 [2011-11-30 08:40 UTC] lunter at interia dot pl
I'm all for it.

example: short switch/case alternative
--------------------------------------

<?php

echo 'ABC'[$i]
// or
echo ['A','B','C'][$i]

?>

is equivalent to long:

<?php

switch ($i) {
    case 0:
        echo "A";
        break;
    case 1:
        echo "B";
        break;
    case 2:
        echo "C";
        break;
}

?>
 [2011-11-30 08:55 UTC] laruence@php.net
Dmitry, could you look at this please? thanks
patch and test list in: https://wiki.php.net/rfc/constdereference

All existing test passed.
 [2011-11-30 08:55 UTC] laruence@php.net
-Assigned To: +Assigned To: dmitry
 [2011-11-30 09:45 UTC] laruence@php.net
The following patch has been added/updated:

Patch Name: req60408.patch
Revision:   1322646333
URL:        https://bugs.php.net/patch-display.php?bug=60408&patch=req60408.patch&revision=1322646333
 [2011-11-30 09:46 UTC] laruence@php.net
The following patch has been added/updated:

Patch Name: const_dereference_001.phpt
Revision:   1322646369
URL:        https://bugs.php.net/patch-display.php?bug=60408&patch=const_dereference_001.phpt&revision=1322646369
 [2011-11-30 09:46 UTC] laruence@php.net
The following patch has been added/updated:

Patch Name: const_dereference_003.phpt
Revision:   1322646387
URL:        https://bugs.php.net/patch-display.php?bug=60408&patch=const_dereference_003.phpt&revision=1322646387
 [2011-11-30 09:46 UTC] laruence@php.net
The following patch has been added/updated:

Patch Name: const_dereference_002.phpt
Revision:   1322646398
URL:        https://bugs.php.net/patch-display.php?bug=60408&patch=const_dereference_002.phpt&revision=1322646398
 [2011-12-27 09:36 UTC] dmitry@php.net
I would say the patch is wrong. It misses the possibility of container_ptr separation in zend_fetch_dimension_address_read(). It also won't work with ZEND_FETCH_DIM_FUNC_ARG.
 [2011-12-27 09:36 UTC] dmitry@php.net
-Assigned To: dmitry +Assigned To: laruence
 [2011-12-27 09:41 UTC] laruence@php.net
dmitry, thanks very much for pointing that out, I will try to fix these issues. :)
 [2012-04-17 02:11 UTC] laruence@php.net
-Status: Assigned +Status: Closed
 [2012-04-17 02:11 UTC] laruence@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.

committed into trunk.
 [2012-07-06 13:07 UTC] bugzilla33 at gmail dot com
When it will be available in final version of PHP?
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Mar 14 22:01:29 2025 UTC