php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70910 extract() breaks variable references
Submitted: 2015-11-13 10:57 UTC Modified: 2015-11-14 09:16 UTC
From: php at maisqi dot com Assigned: laruence (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 7.0.0RC7 OS: Windows 8
Private report: No CVE-ID: None
 [2015-11-13 10:57 UTC] php at maisqi dot com
Description:
------------
extract() sets the values of variables that reference variables, instead of changing the variables that they point to.
For example, say $ref is a reference to $var; if extract() has a "ref" var to inject in the current scope, it changes $ref and does nothing to $var -- it breaks the reference.

NOTE: My tests was on PHP 7RC7 64 bits on Windows, though this probably isn't relevant.

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

$var = 'original value';
$ref =& $var;

$hash = ['var' => 'new value'];

extract($hash);
echo 'ref: ', $ref, '   var: ', $var;


Expected result:
----------------
ref: new value   var: new value

(This is what happens on PHP 5.6/64 bits)

Actual result:
--------------
ref: original value   var: new value

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-11-13 15:39 UTC] laruence@php.net
-Status: Open +Status: Feedback -Assigned To: +Assigned To: laruence
 [2015-11-13 15:39 UTC] laruence@php.net
actually, I think previously behavior seems bug... current behavior is more reasonable, "it overwrite the old value".
 [2015-11-13 16:24 UTC] nikic@php.net
@laruence: My intuition would say that extract() without EXTR_REFS should behave the same was as manually doing the assignments, i.e.:

    extract(['a' => 'b', 'c' => 'd']);
    // should be the same as writing
    $a = 'b';
    $c = 'd';

So in this case the equivalent code without extract() would be:

    $var = 'original value';
    $ref =& $var;
    $var = 'new value';

In which case the result that PHP 5.6 provides is correct.

If EXTR_REFS is specified then all assignments should happen with =&, which breaks references. In this case the current behavior matches the previous one already.
 [2015-11-13 19:13 UTC] php at maisqi dot com
-Status: Feedback +Status: Assigned
 [2015-11-13 19:13 UTC] php at maisqi dot com
I agree with nikic. It should do the same as doing it manually.
 [2015-11-14 03:40 UTC] laruence@php.net
Automatic comment on behalf of laruence@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=fce44a5a131f4d897ed9472fcf9a05efdbaa4d03
Log: Fixed bug #70910 (extract() breaks variable references)
 [2015-11-14 03:40 UTC] laruence@php.net
-Status: Assigned +Status: Closed
 [2015-11-14 03:40 UTC] laruence@php.net
-Status: Closed +Status: Assigned
 [2015-11-14 03:40 UTC] laruence@php.net
okey, I fixed it, make it be consistent with 5.6's behavior

thanks
 [2015-11-14 09:16 UTC] laruence@php.net
-Status: Assigned +Status: Closed
 [2015-11-14 09:16 UTC] laruence@php.net
The fix for this bug has been committed.

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.


 [2015-11-20 01:03 UTC] ab@php.net
Automatic comment on behalf of laruence@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=2aa400a0ffba12a551015e329ab53829d25b3805
Log: Fixed bug #70910 (extract() breaks variable references)
 [2016-07-20 11:35 UTC] davey@php.net
Automatic comment on behalf of laruence@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=fce44a5a131f4d897ed9472fcf9a05efdbaa4d03
Log: Fixed bug #70910 (extract() breaks variable references)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 14:01:29 2024 UTC