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:[email protected]> [email protected]

Cell: (801) 310-1421




_______________________________________________

UPHPU mailing list
[email protected]
http://uphpu.org/mailman/listinfo/uphpu
IRC: #uphpu on irc.freenode.net


_______________________________________________

UPHPU mailing list
[email protected]
http://uphpu.org/mailman/listinfo/uphpu
IRC: #uphpu on irc.freenode.net

Reply via email to