<p>An instance of class A instantiates a couple of other objects, say for example from class B:</p>

<pre><code>$foo = new B();
</code></pre>

<p>I would like to access A's public class variables from methods within B.</p>

<p>Unless I'm missing something, the only way to do this is to pass the current object to the  instances of B:</p>

<pre><code>$foo = new B($this);
</code></pre>

<p>Is this best practice or is there another way to do this?</p>

[{'answer_id': 62242, 'author': 'Hanno Fietz', 'author_id': 2077, 'author_profile': 'https://Stackoverflow.com/users/2077', 'pm_score': 1, 'selected': False, 'text': '<p>I would first check if you are not using the wrong pattern: From your application logic, should B really know about A? If B needs to know about A, a parent-child relationship seems not quite adequate. For example, A could be the child, or part of A\'s logic could go into a third object that is "below" B in the hierarchy (i. e. doesn\'t know about B).</p>\n\n<p>That said, I would suggest you have a method in B to register A as a data source, or create a method in A to register B as an <a href="http://en.wikipedia.org/wiki/Observer_pattern" rel="nofollow noreferrer" title="Wikipedia: Observer pattern">Observer</a> and a matching method in B that A uses to notify B of value changes.</p>\n'}, {'answer_id': 62249, 'author': 'Paul Dixon', 'author_id': 6521, 'author_profile': 'https://Stackoverflow.com/users/6521', 'pm_score': 3, 'selected': True, 'text': '<p>That looks fine to me, I tend to use a rule of thumb of "would someone maintaining this understand it?" and that\'s an easily understood solution.</p>\n\n<p>If there\'s only one "A", you could consider using the registry pattern, see for example <a href="http://www.phppatterns.com/docs/design/the_registry" rel="nofollow noreferrer">http://www.phppatterns.com/docs/design/the_registry</a></p>\n'}, {'answer_id': 76201, 'author': 'Peter Bailey', 'author_id': 8815, 'author_profile': 'https://Stackoverflow.com/users/8815', 'pm_score': 0, 'selected': False, 'text': "<p>Similar to what Paul said, if there's only one A, you can implement that as a singleton. You can then pass the instance of A as an argument to the constructor (aggregation), with a setter method (essentially aggregation again), or you can set this relationship directly in the constructor (composition).</p>\n\n<p>However, while singletons are powerful, be wary of implementing them with composition. It's nice to think that you can do it that way and get rid of a constructor argument, but it also makes it impossible to replace A with something else without a code rewrite.  Peronsally, I'd stick with aggregation, even if using a singleton</p>\n\n<pre><code>$foo = new B( A::getInstance() );\n</code></pre>\n"}, {'answer_id': 883890, 'author': 'Jet', 'author_id': 109480, 'author_profile': 'https://Stackoverflow.com/users/109480', 'pm_score': 0, 'selected': False, 'text': '<pre><code>$foo = new B($this);\n</code></pre>\n\n<p>Code like this unfortunately does not match my needs. Is there any other way to access the parent object properties? </p>\n\n<p>I\'ll try to explain why. We write a game software and some classes have very "unusual" dependencies and influence each other in different ways. That\'s why code sometimes gets almost unsupportable without links to parents in every instance (sometimes even several parents from different contexts i.e. a Squad may belong to Battle and to User etc...).</p>\n\n<p>And now the reason why links don\'t satisfy me. When I generate an output for the client side, I use a kind of serializing objects in XML. It works very nice until it meets recursive references like those links to parents. I can make them protected, but then they loose their usage i.e. (dummy example)</p>\n\n<pre><code>$this-&gt;squad-&gt;battle-&gt;getTeam($tid)-&gt;getSquad($sqid)-&gt;damageCreature(...);\n</code></pre>\n\n<p>The other way - to implement serialization method in every serializable class and call it inside serializer like this:</p>\n\n<pre><code>$obj-&gt;toXML($node);\n$this-&gt;appendChild($node);\n</code></pre>\n\n<p>but that\'s a lot of stuff to write and to support! And sometimes i generate the objects for serializer dynamically (less traffic).</p>\n\n<p>I even think about a hack: to "teach" serializer to ignore some properties in certain classess )). Huh... bad idea...</p>\n\n<p>It\'s a long discussion, but believe me, that Registry and Observer don\'t fit. Are there any other ideas?</p>\n'}]
['https://Stackoverflow.com/questions/62226', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6260/']