S
S
scarah2011-05-31 19:20:13
PHP
scarah, 2011-05-31 19:20:13

Reading properties of another object in php

Good day, Habrousers!
I have the following question for you on OOP in php:
There are two objects of two different classes, how, when executing the function of one, to read information from the property of the other?
Moreover, the relationship between them is as follows:
There is an object $a of class A and an object of class B, A and B do not inherit each other in any way, but one of the properties of class $a (let it be $a->c) is an object of class B (i.e. $ a->c=new B()).

So the essence of the question is that by executing some function $a->c->d(), from this function to get access to the property $a->e, and call the function not B::d(), namely $a->c->d().
Is that possible? If so, how?
Thank you in advance!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ivan, 2011-06-02
@dohlik

Hm... Don't you think that something is wrong in the algorithm you use? Try to describe it here, maybe we'll come up with something.
It is obvious that the object that has the initial data in the maximum volume should perform the action. In this case, it's $a (it contains both an object of class B and a property $a->e). Most likely, it cannot perform all the actions from the $a->c->d() method for the same reason, so $a->c->d() should be called inside our ideal $a->x() method to get data about $a->c.
Of course, you can do all this simply by storing a reference to the parent in the $a->c object, but for me this is more of a crutch.

H
himik, 2011-05-31
@himik

pass to $a->c->d($this)
and inside d($obj) take $obj->e

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question