[UPHPU] Accessing a Parent Object's Other Variables
Justin Carmony
justin at justincarmony.com
Mon Jun 22 13:12:05 MDT 2009
The recursion warning happens because in theory, print_r goes through
a prints out the values for an object. So it prints out everything in
A, which includes a reference to B. So it prints out everything in B,
which includes an A. So it prints everything in A, which has a B.....
So that function would run recursively. Have two objects with
reference to each other isn't a problem, just that the print_r
function will throw those recursions errors.
Justin
On Jun 22, 2009, at 12:57 PM, Kirk Ouimet wrote:
> Hi List,
>
>
>
> I need to be able to access a other variables in a parent class from
> an
> object inside the parent class. Here's what I've cooked up, but it
> throws a
> *RECURSION* warning:
>
>
>
> <?php
>
> Class A {
>
> var $msg = 'Why hello there my old friend';
>
> var $b;
>
>
>
> function __construct() {
>
> $this->b = new B($this);
>
> }
>
> }
>
>
>
> Class B {
>
> var $a;
>
> function __construct($a) {
>
> $this->a = $a;
>
> }
>
> }
>
>
>
> $a = new A();
>
> $b = $a->b;
>
> echo $b->a->msg;
>
> // Output: Why hello there my old friend
>
>
>
> print_r($a);
>
> /* Output:
>
> (
>
> [msg] => Why hello there my old friend
>
> [b] => B Object
>
> (
>
> [a] => A Object
>
> *RECURSION*
>
> )
>
>
>
> )
>
> */
>
> ?>
>
>
>
> Is the *RECURSION* warning bad coding on my part? What's a best
> practice
> here?
>
>
>
> Thanks!
>
>
>
> Kirk Ouimet
>
> <mailto:kirk at kirkouimet.com> kirk at kirkouimet.com
>
> Cell: (801) 310-1421
>
>
>
>
> _______________________________________________
>
> UPHPU mailing list
> UPHPU at uphpu.org
> http://uphpu.org/mailman/listinfo/uphpu
> IRC: #uphpu on irc.freenode.net
More information about the UPHPU
mailing list