php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78263 \ReflectionReference::fromArrayElement() returns null while item is a reference
Submitted: 2019-07-08 13:13 UTC Modified: 2019-07-08 15:25 UTC
From: nicolas dot grekas+php at gmail dot com Assigned: nikic (profile)
Status: Closed Package: Reflection related
PHP Version: 7.4Git-2019-07-08 (Git) OS:
Private report: No CVE-ID: None
 [2019-07-08 13:13 UTC] nicolas dot grekas+php at gmail dot com
Description:
------------
See reproducer:

Test script:
---------------
$var = function &() {
        $var = [];
        $var[] = &$var;

        return $var;
};

$var = [$var()];

function isref1(array $a, string $k)
{
        return null !== \ReflectionReference::fromArrayElement($a, $k);
}

function isref2(array $a, string $k)
{
        $v = $a[$k];
        $b = $a;
        $b[$k] = $c = new \stdClass();

        if ($a[$k] === $c) {
                $a[$k] = $v;

                return true;
        }

        return false;
}

var_dump($var);
var_dump([isref1($var[0], 0), isref2($var[0], 0)]);


Expected result:
----------------
array(1) {
  [0]=>
  array(1) {
    [0]=>
    *RECURSION*
  }
}
array(2) {
  [0]=>
  bool(false)
  [1]=>
  bool(true)
}


Actual result:
--------------
array(1) {
  [0]=>
  array(1) {
    [0]=>
    *RECURSION*
  }
}
array(2) {
  [0]=>
  bool(true)
  [1]=>
  bool(true)
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-07-08 13:33 UTC] nikic@php.net
Null is returned here because it is an rc=1 reference, as requested in bug #77951. Does that mean that we should return non-null for rc=1 references after all?
 [2019-07-08 15:25 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2019-07-08 15:25 UTC] nikic@php.net
https://github.com/php/php-src/commit/428cfdd1810b17f0064b7691276f0eb92dc963b6 makes ReflectionReference return non-null for rc=1 references and adds a separate method to check the refcount, so the user can decide himself whether or not they want to deal with singleton references.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Mon Dec 09 05:01:26 2019 UTC