in reply to Getting Confused with the '->' operator

In your first example: $interface->handler($client)->parse($local_file_name); the method that is called is the parse method of the object referenced by: $interface->handler($client). The arrow operator associates left to right, hence the above result.

Your second example

$Person->name('Homer') ->job('safety inspector') ->wife('Marge') ->fav_food('Duff');

seems to be just plain weird. I might be (and almost certainly am) missing something - the same thing as you in that case :) - but I read this as "execute the method fav_food on the object returned by executing wife on the object returned by executing job on the object returned by executing name on the thingy referenced in $person" (arguments left out to save what little shreds of clarity remaining). Beats me. Help.

CU

Robartes -

Replies are listed 'Best First'.
Re: Re: Getting Confused with the '->' operator
by charnos (Friar) on Oct 01, 2002 at 13:34 UTC
    You are right, it does appear to be plain weird. It should evaluate differently than The_Don expected, with the -> operators operating left to right, so you did read it correctly. BrowserUK made a note about the Java APIs working this way, perhaps name(),job(),wife(), and (presumably)fav_food() all return the object that called it, that much is not clear.
    Personally, when writing classes I tend to have my mutators (set()s) return nothing, making a clear distinction between accessors and mutators (except in certain circumstances).