/    Sign up×
Community /Pin to ProfileBookmark

Object references – stuck….

Hi all.

I’m stuck with trying to reference objects. Here is the scenario:
There are two objects Foo and Bar.
In Foo’s constructor the Bar is created and reference to Foo is passed to it.
The Bar’s constructor stores the reference to Foo.
Then Foo’s constructor calls Bar’s method that in turn uses the refence to Foo to call Foo’s method that updates Foo’s property:

[code=php]class Foo
{ var $test;
function add()
{ $this->test++;
}
function Foo()
{ $this->test = 0;
$bi = new Bar($this);
$bi->CallFoo();
}
}

class Bar
{ var $foo;
function CallFoo()
{ $this->foo->add();
}
function Bar(&$fp)
{ $this->foo = $fp;
}
}

$fi = new Foo();
print_r($fi->test); [/code]

I expect the print_r($fi->test) to return 1 not 0
?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NaemoJun 27.2004 — Hi

Bear with me because i may not explain this very well. If you add echo statements to watch the variables it begins to make sense.

[code=php]

class Foo{
var $test;
function add(){
$this->test++;
echo '4 ($this->test) : '.$this->test."<br/>";
}
function Foo(){
$this->test = 0;
echo '1 ($this->test) : '.$this->test."<br/>";
$bi = new Bar($this);
echo '3 ($this->test) : '.$this->test."<br/>";
$bi->CallFoo();
echo '6 ($this->test) : '.$this->test."<br/>";
echo '7 ($bi->foo->test) : '.$bi->foo->test."<br/>";
$this->test = $bi->foo->test;
}
}
class Bar{
var $foo;
function CallFoo(){
$this->foo->add();
echo '5 ($this->foo->test) : '.$this->foo->test."<br/>";
}
function Bar(&$fp){
$this->foo = $fp;
echo '5 ($this->foo->test) : '.$this->foo->test."<br/>";
}

}

$fi = new Foo();
print_r('F ($fi->test): '.$fi->test);


[/code]


because the new appended test value was attached to the variable $bi! i dont really know how to explain it but if you view the code it should make sense.

? ? ?
Copy linkTweet thisAlerts:
@shimonJun 28.2004 — [i]Originally posted by Vladdy [/i]

I expect the print_r($fi->test) to return 1 not 0

? [/QUOTE]


Even more confusingly, run it under PHP5 and it will return 1...there's a good reason for that though.

Anyway, in PHP4, the problem is here:

[code=php]<?php

$this->foo = $fp;

?>[/code]


You're taking a copy of $fp rather than a reference to it. So, change that to:

[code=php]<?php

$this->foo =& $fp;

?>[/code]


and it happily prints out 1.

The moral of the story is that you need to be utterly careful when moving objects around in PHP4 - you need to be pretty aggressive in order not to end up with numerous copies of an object. PHP5 "fixes" that by passing objects by reference by default.


[EDIT]btw, convention (and good style) dictates that your constructor _really_ should be the first function you declare within the class. Just though I should mention that ?
Copy linkTweet thisAlerts:
@moondanceJun 28.2004 — is there any way that you can print out or get an array of all the objects or even variables that are in the computers memory at run-time?

see i've been developing with objects recently, and this post has got me worried in case i have thirty odd objects floating around behind the scenes ?
Copy linkTweet thisAlerts:
@shimonJun 28.2004 — There's [URL=http://www.php.net/get_defined_vars]get_defined_vars()[/URL] which (at least for PHP5RC4/Linux) also returns all defined objects:

So:

[code=php]<?php

echo "<pre>";
print_r(get_defind_vars());
echo "</pre>";

?>[/code]


I just dropped that into the code im working on right now... eye-opening stuff really :eek:
Copy linkTweet thisAlerts:
@VladdyauthorJun 28.2004 — Thanks all.
×

Success!

Help @Vladdy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...