Mysterious arrows of PHP.
February 25, 2008 7:46 PM   Subscribe

I'm puzzled by the use of "->" in PHP. I keep seeing it used in php scripts, but I can't figure out how to google for it. Without knowing what it is called or the name of the operation it represents, it remains a mystery. What does it do, and when is it useful? Can anyone enlighten me?
posted by BishopsLoveScifi to Computers & Internet (9 answers total) 3 users marked this as a favorite
 
It accesses methods and member variables of an object.

When in doubt, check the manual. Specifically, the sections on classes and objects in PHP4 and PHP5.
posted by teraflop at 7:51 PM on February 25, 2008


Best answer: It's how PHP let's you reference a member of some class. If there was some class reference stored in the variable $foo and there was a member function 'noWay()' you could call it by writing '$foo->noWay()'.

I think that's it? PHP might have some other black-magic uses for it too.
posted by thebigdeadwaltz at 7:51 PM on February 25, 2008


it's the namespace separator or object access operator. you use it to reference member data of an object.
posted by dorian at 7:52 PM on February 25, 2008


Response by poster: Thanks for the unexpectedly quick responses!

It gave me enough handy phrases to point me to a relevant article - I didn't appreciate what 'it's a namespace separator' actually meant until the article explains it as akin to the / in directory structures. Stupid gaps in my programming vocabulary :)

I get it now....
posted by BishopsLoveScifi at 8:29 PM on February 25, 2008


Question already answered, but I'd like to throw in that I 'got' this (in C, it's the same) when I started thinking

foo->bar()
bar, which bar? follow the arrow backwards, ahhh foo's barr

of course following arrows backwards is non intuitive,,,but thus easy to remember ("stupid arrows")
posted by oblio_one at 11:20 PM on February 25, 2008


Best answer: The orthodox name for this would be the dereference operator or indirection operator.

But Mysterious Arrow is cool too. That sounds like the name of a superhero.

Calling it the namespace separator would be incorrect. Object and variable aggregation via pointers, and namespaces look similar but they are not the same thing. The namespace separator in PHP is ::.

Also note that as the dereference operator “->” does a great deal more than access the members of objects. But that's too extensive a topic to get into here - the quick tip is to read up on the topic of “pointers” in general in programming languages. If I recall correctly most of the things that can be done with a pointer in any language can also be done in PHP.
posted by XMLicious at 12:08 AM on February 26, 2008 [1 favorite]


I can't figure out how to google for it

Searching for php dash-greater-than gets you there, though the answers above are clearly far more helpful.
posted by Busy Old Fool at 1:16 AM on February 26, 2008


In many other object oriented languages, '.' is synonymous with '->'.
posted by heresiarch at 7:00 AM on February 26, 2008


In many other object oriented languages, '.' is synonymous with '->'.

Not quite. The dot is used for member access. The arrow dereferences the element to its left then accesses the member to its right. (i.e. a->b is short hand for (*a).b.)
posted by chunking express at 7:40 AM on February 26, 2008


« Older Is it time to cut my losses?   |   Should I get a PS3? Newer »
This thread is closed to new comments.