$a->b->c reference ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zb21
Forum Newbie
Posts: 1
Joined: Tue Apr 20, 2010 8:54 pm

$a->b->c reference ?

Post by zb21 »

Hello,

This is probably something simple, but I can not find an aswer to my question...
I define a class:

class myClass {
var $pointer;
};

Then I do the following:

$myClassRef = new myClass;
$myClassRef->pointer = new AnotherClass;

So far everything is good. Then when I do the following:

$x = $myClassRef->pointer;
echo $x->elementofAnotherClass;

everything is good.
If I try however something, which - to me - would be intuitive, that is:

echo $myClassRef->pointer->elementofAnotherClass;

I get a 'Catchable fatal error: Object of class System could not be converted to string in ...'.
My PHP version is 5.2.13 but this did not work in version 4.xx either.

Could somebody help me with this, please?
Thank you so much,
ZB21
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: $a->b->c reference ?

Post by Eran »

That property must be another object. You can't simply echo an object - PHP does not understand that and gives you the "object could not be converted to string" error.

By the way, there are no pointers in PHP, just references.
Post Reply