php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46885 pass by reference does not return object created in function
Submitted: 2008-12-16 21:26 UTC Modified: 2010-03-08 20:53 UTC
From: Chowarmaan at gmail dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5.2.8 OS: *
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: Chowarmaan at gmail dot com
New email:
PHP Version: OS:

 

 [2008-12-16 21:26 UTC] Chowarmaan at gmail dot com
Description:
------------
Calling a function in a class that accepts a variable by reference, then checks the variable and creates an object for it will not allow the object to be returned outside of the function.


Reproduce code:
---------------
<?php
class TEST_CLASS
{
    function __construct()   {     }

    function GetResponse(&$Response, $Timeout = 0)
    {
        if (!($Response instanceof TEST_CLASS2 ))
            $Response = new TEST_CLASS2();

        $Response->SetText_('Testing Complete');
        return TRUE;
    }
}

class TEST_CLASS2
{
	public $Variable;
	
	public function SetText_($s)
	{
		$this->Variable = $s;
	}
}

$Test = new TEST_CLASS();
$Test->GetResponse($Response, 100);   // Fails
$Test->GetResponse(&$Response, 100);  // Works

echo $Response->Variable . "\n";    
?>    


Expected result:
----------------
$Response should be of type TEST_CLASS2 and the echo should return the text:
Testing Complete

Actual result:
--------------
$Response is a NULL

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-12-17 02:44 UTC] jani@php.net
How about you comment out the latter call and let the script work 
like it does?
 [2008-12-17 23:14 UTC] Chowarmaan at gmail dot com
Both lines were added to show what works and what does not.  The first call is the desired call by the program and what I can see in the PHP manual.  However, $Response does not become an object of TEST_CLASS2 when the GetResponse function ends.

The second call, &$Response does maintain the TEST_CLASS2 object type.  The second line should not be in the script, it is just there to illustrate the problem.  The first call to GetResponse() is the correct call by the language syntax, but it has the bug.
 [2008-12-18 14:26 UTC] jani@php.net
Are you really using PHP 5.2.8? Because this works just fine for me with both styles. Here's my simplified script:

<?php
class tst
{
  function get(&$r , $t = 0)
  {
    if (!($r instanceof tst2 ))
      $r = new tst2();
    $r->set('Testing Complete');
  }
}
class tst2
{
        public $var;
        public function set($s)
        {
                $this->var = $s;
        }
}
$t = new tst;
$t->get($resp, 100);
var_dump($resp->var);
$t->get(&$resp, 100);
var_dump($resp->var);
?>

And output:
# php -dallow_call_time_pass_reference=on t.php
string(16) "Testing Complete"
string(16) "Testing Complete"

# php -dallow_call_time_pass_reference=off t.php

Warning: Call-time pass-by-reference has been deprecated in t.php on line 25
string(16) "Testing Complete"
string(16) "Testing Complete"

 [2008-12-18 15:28 UTC] Chowarmaan at gmail dot com
I retried this as you showed, and then again in my code sample.  I can not reduplicate this problem.

I have been using the 5.2.8 since it came out as it is on my development machine, and the only PHP on that machine.  I confirmed this with the first test and this test using the php -v and phpinfo();

Originally I noticed the problem using Zend 5.5.1, but I can not reduplicate the problem there either.
 [2010-03-08 20:53 UTC] Chowarmaan at gmail dot com
Can not duplicate at this time with 5.2.13.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 15 23:01:33 2025 UTC