|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Dec 07 01:00:01 2025 UTC |
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"