php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47955 ReflectionClass::newInstanceArgs() fails with references
Submitted: 2009-04-12 21:54 UTC Modified: 2018-10-02 16:36 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: richardcook at gmail dot com Assigned:
Status: Not a bug Package: Reflection related
PHP Version: 5.*, 6CVS (2009-05-14) OS: *
Private report: No CVE-ID: None
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
8 - 7 = ?
Subscribe to this entry?

 
 [2009-04-12 21:54 UTC] richardcook at gmail dot com
Description:
------------
ReflectionClass::newInstanceArgs throws a constructor fail warning 
and does not create the class if the class' constructor has variable 
references in it

Reproduce code:
---------------
http://pastebin.com/f4a147910

Expected result:
----------------
Array
(
    [x] => 1
    [y] => 2
    [z] => 3
)
Foo Object
(
    [arr] => Array
        (
            [x] => 1
            [y] => 2
            [z] => 3
        )

)
Bar Object
(
    [foo] => Foo Object
        (
            [arr] => Array
                (
                    [x] => 1
                    [y] => 2
                    [z] => 3
                )

        )

    [arr] => Array
        (
            [x] => 1
            [y] => 2
            [z] => 3
        )

)

Actual result:
--------------
Warning: Invocation of Bar's constructor failed in [code path] on 
line 9

Fatal error: Call to a member function mod() on a non-object in [code 
path] on line 36

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-04-13 17:52 UTC] jani@php.net
<?php
class Foo {
  function __construct (&$arr) {
    $this->arr = &$arr;
  }
  function createInstance () {
    $reflectionClass = new ReflectionClass("Bar");
    return $reflectionClass->newInstanceArgs(array($this, $this-
>arr));
  }
  function mod($key, $val) {
    $this->arr[$key] = $val;
  }
}

class Bar {
  function __construct (&$foo, &$arr) {
    $this->foo = &$foo;
    $this->arr = &$arr;
}
  function mod($key, $val) {
    $this->arr[$key] = $val;
  }
}

$arr = array();

$foo = new Foo($arr);

$arr["x"] = 1;

$foo->mod("y", 2);

$bar = $foo->createInstance();

$bar->mod("z", 3);

var_dump($arr,$foo,$bar);

?>


 [2009-05-21 16:37 UTC] lbarnaud@php.net
It works when the elements of the array are references:

$reflectionClass->newInstanceArgs(array(&$this, &$this->arr));



 [2018-10-02 16:36 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2018-10-02 16:36 UTC] nikic@php.net
The current behavior here is correct and intended. As mentioned, you need to use references in the array to allow this to work in any reasonable fashion.

Since PHP 7.1 the call will be performed despite the reference warnings, though this may be tightened again in the future.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri May 09 08:01:35 2025 UTC