tags:

views:

143

answers:

6

Hi guys,

is there a way to traverse an object to get the parent object data? With "parent object" I don't mean the parent class, but literally object. Here an example, in a javascripty world :-) :

$parent->test = "hello world!";

$parent->child = new B();

It would be great If I could access all the data from parent in the child object:

class B{

//I know this doesn't exists, but it's what I wanted do do
    function B(){
        $this->parent = $this->parent();
        echo $this->parent->test; //it would ouput "hello world"
    }
}

For now my solution is to pass the parent object to the child (as a reference) or to make the parent global. Do you have any better solution?

Thanks!

A: 

No.

You can access variables from the superclass using $this:

<?php
class A {
    public function __construct()
    {
        $this->foo = 'hello';
    }
}

class B extends A {
    public function printFoo()
    {
        echo $this->foo;
    }
}

$b = new B();
$b->printFoo();
?>
Sjoerd
This was not the question. He doesn't want to access the superclass.
Hinek
I think this answer is fine ("No."). Sjoerd also provides an alternative solution.
Martin Wickman
+1  A: 

Passing the parent to the child is the better solution. Though it's a somewhat undesirable symbiotic relationship.

Should B() have any knowledge of the object it's an attribute of? More than likely not. These are only loosely related via composition.

Why does B() need to be an attribute of it's parent? Do you need to have this implementation in B() or should it be part of the parent?

Greg K
After a month of javascript, where traversing object is normal, I find php just too "static" in this case.Think about an mvc framework where some properties are defined on the way and not by default and at the end the main controller class calls a method that fires a child object creation. If I use the "extends" method by Sjoerd, I cannot then access the properties created on the way but just the properties defined on the top of the class.
AntonioCorrenti
So you need to pass around an object that holds the information you're building up? Maybe you should re-think this design to reduce coupling - would a dependency injection container help here?
Greg K
A: 

Check out late static binding in PHP.

powtac
He's not using inheritance here, just composition.
Greg K
A: 

I'm pretty sure, that PHP does not have something like that.

Your solution of passing the parent-object to the child is the best solution, I think. You should consider to set your Parent-property only in the constructor of the child to prevent multiple parents from having the same child.

Hinek
+7  A: 

There is no way to invoke

$parent->test = "hello world!";
$parent->child = new B();

and automatically have a reference to $parent in B.


Generally, there is four ways to structure your classes:

1. Aggregate the parent object via Injection, e.g.

class B{
     protected $parent;
     public function __construct($parent) {
         $this->_parent = $parent;
     }
     public function setParent($parent) {
         $this->_parent = $parent;
     }
     public function accessParent() {
        $this->_parent->someMethodInParent();
     }
}

Use constructor injection when the object has to have a parent when it's created. This is a has-a relationship and it creates a very loose coupling. There is no hardcoded dependencies in B, so you can easily swap out the Parent instance, for instance with a Mock when UnitTesting. Using Dependency Injection will make your code more maintainable.

In your UseCase, you'd pass $parent to B when creating B:

$parent->child = new B($parent);

2. Use Composition

class B{
     protected $_parent;
     public function __construct() {
         $this->_parent = new Parent;
     }
     public function accessParent() {
        $this->_parent->someMethodInParent();
     }
}

This is also a has-a relationship, but couples the Parent class to B. It's also not an existing Parent instance, but a new instance. From the wording I find it somewhat odd to have a parent created by the child. Use this, when dependency is a class that is not considered to exist outside of the root class, but is part of the whole thing it represents.

For your UseCase, there is no way of doing $parent->child = new B(); and know what parent is when using this approach, unless $parent is a Singleton. If so, you could get the Singleton instance, e.g. Parent::getInstance() to achieve what you want, but note that Singletons are not everyone's favorite pattern, e.g. hard to test.

3. Use Inheritance

class B extends Parent {
     public function __construct() {
         parent::__construct();
     }
    public function accessParent() {
        $this->someMethodInParent();
    }
}

This way you create an is-a relationship. All public and protected methods and properties from the Parent class, (but not of a specific instance) will be available in B and you can access them via the $this keyword of the B instance.

For your UseCase, this approach is not working, as you don't have to have an instance of Parent at all, but B would encapsulate everything of Parent when it's created

$b = new B;

4. Use global keyword

class B extends Parent {
    protected $_parent;
    public function __construct() {
        global $parent;
        $this->_parent = $parent;
    }
    public function accessParent() {
        $this->_parent->someMethodInParent();
    }
}

The global keyword imports global variables into the current scope. In general, you should avoid using the global keyword in an OO context, but use one of the other three methods above, preferably the first one. While it's a language feature, it's frowned upon - although it is the next closest thing to the first one, e.g.

$parent->child = new B();

Anyway, hope that helps.

Gordon
Nice examples. Seems to cover all cases as well. Maybe one could use closures (in php 5.3) somehow though?
Martin Wickman
I think you are right (this is the way i did it).I like the protection on the top, I will implement it in my framework.After a month of javascript I was just feeling there was another way to do it.The persistence of some object, less data and class rendundancy, this is what I'm missing in php. for example the "privacy" object should always be accessible from every class, because is fundamental to define if a user (another important persistent object for me) can perform an action in (for example) a social networking app.
AntonioCorrenti
@Antonio Well, what can I say, PHP is not JavaScript.
Gordon
@wic hmm, how so? I can only think of odd ways of doing it with closures, though I agree it'd be interesting to see a nice approach.
Gordon
Fabien Potencier (author of the Symfony framework) has some good slides on DI containers in PHP (5.3): http://www.slideshare.net/fabpot/dependency-injection-with-php-53
Greg K
A: 

You can avoid this situation if you follow the advice here: http://misko.hevery.com/2008/08/01/circular-dependency-in-constructors-and-dependency-injection/

rojoca