Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hi all. I've a problem with variable references which is really driving me crazy.
First of all, I'm running my code on a Linux server with Apache/2.0 and PHP 4.4.4.
Now, the code.Code: Select all
<?php
$refs = array();
class MyTest1
{
var $value = 0;
function MyTest1()
{
global $refs;
$refs[] = &$this;
$this->value = 1234;
}
}
class MyTest2
{
var $value = 0;
function MyTest2()
{
global $refs;
$refs[] = &$this;
}
function SetValue()
{
$this->value = 1234;
}
}
$inst1 = new MyTest1;
$inst2 = new MyTest2;
$inst2->SetValue();
print_r($refs);
?>Both the classes have a data member ($value) whose value is initialized to 0 and will be changed during execution.
The only difference between the two classes is that MyTest1 sets the new value ($this->value = 1234) in the constructor while MyTest2 sets the new value in another member function (SetValue()) which will be called from outside the class after instantiation ($inst2->SetValue());
AFAIK the two operations should produce the same result, but, if you try to execute this code, you will receive this result:
Array ( [0] => mytest1 Object ( [value] => 1234 ) [1] => mytest2 Object ( [value] => 0 ) )
As you can see, the value of the data member for MyTest2 reference is still 0! I really don't understand what I'm doing wrong.
Any help will be greatly appreciated.
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]