php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71603 compact() maintains references in php7
Submitted: 2016-02-15 23:24 UTC Modified: 2016-02-16 01:16 UTC
From: tim at zaremedia dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 7.0.3 OS: Debian 8
Private report: No CVE-ID: None
 [2016-02-15 23:24 UTC] tim at zaremedia dot com
Description:
------------
When compact() is used with references, those references are maintained in the new array in php7 and not in 5.6

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

$foo = 45;
$foo_reference =& $foo;

$array = compact('foo_reference');

print_r($array);

$foo = 50;

print_r($array);

Expected result:
----------------
PHP 5.6.17
----------
    Array
    (
        [foo_reference] => 45
    )
    Array
    (
        [foo_reference] => 45
    )


Actual result:
--------------
PHP 7.0.3
---------
    Array
    (
        [foo_reference] => 45
    )
    Array
    (
        [foo_reference] => 50
    )

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-02-16 00:58 UTC] requinix@php.net
-Status: Open +Status: Feedback -Package: Unknown/Other Function +Package: Scripting Engine problem
 [2016-02-16 00:58 UTC] requinix@php.net
PHP 7's changes also fixed references to work more consistently.

Isn't the new behavior better? Since the variable was a reference, shouldn't the array use a reference too?
 [2016-02-16 01:16 UTC] tim at zaremedia dot com
-Status: Feedback +Status: Open
 [2016-02-16 01:16 UTC] tim at zaremedia dot com
I'm not sure if it's better or not but it was a surprising change between the two versions. I'd argue that if this is the "correct" way then:

$array2 = ['foo_reference' => $foo_reference];

should also behave in a similar fashion, which it does not.
 [2016-02-16 03:03 UTC] laruence@php.net
Automatic comment on behalf of laruence@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=0fccd154bdb27476289bab18a9112fb7b20ae607
Log: Fixed bug #71603 (compact() maintains references in php7)
 [2016-02-16 03:03 UTC] laruence@php.net
-Status: Open +Status: Closed
 [2016-07-20 11:33 UTC] davey@php.net
Automatic comment on behalf of laruence@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=0fccd154bdb27476289bab18a9112fb7b20ae607
Log: Fixed bug #71603 (compact() maintains references in php7)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 08:01:29 2024 UTC