tags:

views:

16

answers:

1

I think my title about says it all. During the __construct()-tion of $classA, $classB is instantiated. When $classB is instantiated it needs to access to another class inside $classA (i.e. $classA->classC). $classC has already been instantiated in $classA.

Inside the __construct() function of $classB I am attempting to do something like Global $classA; so that I can get something from $classA->classC->method(). Obviously $classA hasn't completed instantiation yet so it isn't available to be imported from the Global scope.

What I have done to work around not being able to access $classA via Global is to pass $this as an argument to $classB when it is instantiated in $classA. This is messy but it works. I would like to try to avoid the messy-ness and hackish feel of passing $this.

Does anyone have any ideas how to better accomplish what I am trying to do?

I know that this is kind of complex so if anyone has any ideas how I could clarify it I will be happy to oblige!

+1  A: 

Actually, I would say that passing $this to ClassB is fine - I think it's a totally reasonable way to recursively nest class instances.

More importantly, is there a reason you are nesting recursively like this? If you could reorganize the classes to not depend on one another in this way at construction, you could simplify the whole problem.

(I can't really say for sure without seeing code, and I'm not 100% sure I understand the problem)

willoller