Hey Guys I have a quick question I have an object that contains another object and I am having difficulties referencing the function from the 2nd object.. I thought this code would work
$number = $this->object_one->method_inside_object_one()->method();
now method_inside_object_one() is a method in object_one, that reutrns an Object, that object has a method "method()"
What do I do?
It seems I hd to break it down like this, in order to get it to work
$temp = $this->object_one->method_inside_object_one();
$temp = $temp->method();
Any suggestions would be greatly appreciated, thanks
Jeff.
OOP Question about referencing objects from objects
Moderator: General Moderators
That has got to be the most convoluted thing i've ever heared. And I'm still not 100% sure I understood it. LOL!!!
In a case where the logic gets so convoluted, you really need to sit down and consider refactoring.
Anyway, may the below will work.
Of course, we know that things inside parenthesis should execute first. In this case, the idea is that your object will be returned and the method run giving back your "number" in the end.
Sorry I can't test this right now as I'm on my Win Boxen getting ready to do some gaming.
Cheers,
BDKR
In a case where the logic gets so convoluted, you really need to sit down and consider refactoring.
Anyway, may the below will work.
Code: Select all
$number=($this->object_one->method_inside_object_one())->method();Sorry I can't test this right now as I'm on my Win Boxen getting ready to do some gaming.
Cheers,
BDKR
I tried the parenthesis and it didn't work for some reason :S
Maybe only in php 5 that works? I just find it odd that you can't get reference to an object, then access methods within that object all on one line. i have never really explored objects much though with php mainly with java and c++, but now I am really starting to code everything as oop with php, I keep running into problems though
It doesn't seme to be true oop, although I guess php 5 fixes a lot of this.
Maybe only in php 5 that works? I just find it odd that you can't get reference to an object, then access methods within that object all on one line. i have never really explored objects much though with php mainly with java and c++, but now I am really starting to code everything as oop with php, I keep running into problems though
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland