php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51174 error "expected to be a reference" when $this referenced in an array property
Submitted: 2010-03-01 00:08 UTC Modified: 2020-03-10 08:46 UTC
Votes:27
Avg. Score:4.2 ± 0.9
Reproduced:20 of 20 (100.0%)
Same Version:14 (70.0%)
Same OS:5 (25.0%)
From: skrol29forum at gmail dot com Assigned: cmb (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.3 OS: Win7
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: skrol29forum at gmail dot com
New email:
PHP Version: OS:

 

 [2010-03-01 00:08 UTC] skrol29forum at gmail dot com
Description:
------------
When $this is stored by reference in a PHP array, itself stored in a property, then function call_user_func_array() is able to recognize the reference only if called in the same local context where $this is referenced. In other words, the reference seems to be lost for call_user_func_array().

The bug does not occur with PHP 5.2,
It does occur with both PHP 5.3.0 and PHP 5.3.1. Not yet tested with PHP 5.3.2

Note that if we use a global variable instead of a property, then the bug does not occur.



Reproduce code:
---------------
<?php

function f_test(&$obj) {
	$obj->prop++;
	echo "prop=".$obj->prop." <br>\r\n";
}

class clsTest {
	public $prop = 0;
	public $arg_array = false;
	function meth() {
		if ($this->arg_array===false) $this->arg_array = array(&$this);
		echo 'arg_array[0] '.(($this->arg_array[0]===$this) ? ' is the same instance' : ' is not the same instance').' than $this'."  <br>\r\n";
		call_user_func_array('f_test', $this->arg_array);
	}
}

$oTest = new clsTest;
$oTest->meth(); // OK
$oTest->meth(); // ERR

?>

Expected result:
----------------
arg_array[0] is the same instance than $this
prop=1
arg_array[0] is the same instance than $this

Warning: Parameter 1 to f_test() expected to be a reference, value given in D:\www\bug.php on line 14

Actual result:
--------------
arg_array[0] is the same instance than $this
prop=1
arg_array[0] is the same instance than $this
prop=2

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-03-01 08:24 UTC] skrol29forum at gmail dot com
I've inverted "Expected result" and "Actual result".
 [2010-06-21 00:03 UTC] felipe@php.net
-Package: Class/Object related +Package: Scripting Engine problem -PHP Version: 5.3.1 +PHP Version: 5.3
 [2010-06-21 00:03 UTC] felipe@php.net
I can reproduce it.

array(1) {
  [0]=>
  &object(clsTest)#1 (2) {
    ["prop"]=>
    int(0)
    ["arg_array"]=>
    *RECURSION*
  }
}
prop=1 <br>
array(1) {
  [0]=>
  object(clsTest)#1 (2) {
    ["prop"]=>
    int(1)
    ["arg_array"]=>
    *RECURSION*
  }
}
PHP Warning:  Parameter 1 to f_test() expected to be a reference, value given
 [2010-07-23 01:43 UTC] heis dot turtlemad at gmail dot com
Same failure running Php5.3.2 on FreeBSD8

david:~>php test.php
arg_array[0]  is the 
same instance than $this  <br>
prop=1 <br>
arg_array[0]  is the 
same instance than $this  <br>

Warning: Parameter 1 to f_test() expected to be a reference, value given in /usr/home/david/test.php on line 15
 [2010-09-15 01:15 UTC] jeremy at tuxmachine dot com
See also bug 50394.
 [2015-08-18 18:57 UTC] cmb@php.net
JFTR: the issue has been solved as of PHP 7, see
<https://3v4l.org/PSGk9>.
 [2020-03-10 08:46 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2020-03-10 08:46 UTC] cmb@php.net
> the issue has been solved as of PHP 7

So closing.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Nov 24 15:01:36 2024 UTC