Someone recently asked a question on the Professional PHP Programmers Google Group that I thought was interesting. This question interested me for a few reasons:
- The PHP manual is a very good manual and usually covers things like this; and,
- I remember asking this very question when I first started developing.
Hi all, I’m new to the mailing list, and new to php, and new on programming.
Here I go:
I have a question but I was unable to find an answer for this on the web. Because I have no Idea how to ask this on a search engine.
What’s the meaning of -> and => on php?
As good as the PHP manual is, and as well documented as the operators in PHP are, the documentation on these two operators is lacking severely. Even naming is horribly lacking for these two operators, as a recent Twitter chat shows:
- RobertGonzalez: Ok PHP development peeps, what is the official name of the “=>” operator? I have been calling the array assignment operator.
- daNanner: RT @RobertGonzalez: Ok PHP development peeps, what is the official name of the “=>” operator? I have been calling the array assignmen …
- RobertGonzalez: And while we are at it, what is the ‘->’ operator officially called. I thought it was scope resolution, but it seems that :: is actually SR.
- isa247: @RobertGonzalez associative array operator thingamabob
- isa247: @RobertGonzalez the object operator whatchamacallit
- elazar: @RobertGonzalez That’s a damned good question. WTF *is* the -> operator called? Doesn’t seem to be in the PHP manual.
- montythestrange: @elazar @RobertGonzalez I usually go with “dereference operator”, but when saying “$foo->bar()” I’d say “foo pointer bar”
- isa247: @RobertGonzalez ummm… but I could be wrong, I just asked my php guy and he told me ‘an arrow pointing to the right’ lol
- ideogon: @RobertGonzalez I believe that :: is “paamayim nekudatayim”? Trying to wing that from memory w/o Google. Hebrew for “double colon”?
- BinaryKitten: RT @elazar: @RobertGonzalez That’s a damned good question. WTF *is* the -> operator called? Doesn’t seem to be in the PHP manual.
- ideogon: @RobertGonzalez I was close! http://tinyurl.com/z87sg
- RobertGonzalez: @elazar For arrays, from the manual: “The parameters can be given an index with the => operator.” Even they can’t name it.
- RobertGonzalez: @elazar The object arrow, though, have no idea. Can’t find that anywhere.
- RobertGonzalez: @montythestrange I have heard dereference used before. Maybe I can use that.
- RobertGonzalez: @isa247 Yeah, my coworker just called it the “equals sign with a greater than sign next to it” operator.
- RobertGonzalez: @ideogon Yes, the :: is named paamayim nekudatayim. But it is also called the scope resolution operator in the manual.
- montythestrange: @RobertGonzalez Although technically the dereference operator is something completely different in C++, since it actually has real pointers.
- RobertGonzalez: @ideogon http://tinyurl.com/5dksw4 For Paamayim nekudotayim/Scope Resolution.
- NetDiva: @robertgonzalez => as an operator is greater than/equal to. It’s also used for to assign array values to keys. -> is the OOP arrow operator.
- NetDiva: @robertgonzalez Was that what you were asking?
- RobertGonzalez: @NetDiva I was asking for the official names of the “=>” and “->” operators in PHP.
- RobertGonzalez: @rasmus Dude, do you know the official names of the “=>” and “->” operators in PHP?
- RobertGonzalez: @NetDiva And I think greater than or equal to is >=. At least in PHP.
- rasmus: @RobertGonzalez Don’t think we ever really named them. Something like ‘associate’ and ‘element of’ perhaps, but I am just making it up
- thynctank: @RobertGonzalez as it is in most languages
- thynctank: @RobertGonzalez isn’t `->` the deference operator? Oh wait, that’s C with actual pointers
- RobertGonzalez: @thynctank Yeah. Tried that, didn’t seem to fit
- RobertGonzalez: @rasmus So can I name them then, since they don’t have a name yet?
- barryaustin: @RobertGonzalez @elazar PHP token reference here: http://us.php.net/tokens
- elazar: @RobertGonzalez I’m going with @BarryAustin on this one: when in doubt, Use the Source.
-> object operator, => double arrow operator. - RobertGonzalez: @barryaustin So then double array “=>” and object operator “->”?
- RobertGonzalez: @elazar from @rasmus: Don’t think we ever really named them. Something like ‘associate’ and ‘element of’ perhaps, but I am just making it up
- RobertGonzalez: @elazar But sounds logical. I have used the term object operator before, but always referred to double arrow as array assignment.
- elazar: @RobertGonzalez *shrug* They’re named as tokens in the source, so apparently somebody did.
- jlleblanc: @RobertGonzalez @elazar @rasmus Although not an operator, I have to give @barryaustin credit for helping us name the ‘bracketurd’
- leleu: @RobertGonzalez if you find those oper names, please RT them. Wiki says -> is “member by pointer”, at least in C++
- RobertGonzalez: @elazar True. I am down. I’ll use the token names.
- RobertGonzalez: @barryaustin Thanks for the linkage dude. Helped put me in the right direction.
- RobertGonzalez: RT @barryaustin: @RobertGonzalez @elazar PHP token reference here: http://us.php.net/tokens #unknownphparrowoperatornames
- sweatje: @RobertGonzalez I sometimes call => associative operator from associative array. -> I call the object operator.
- RobertGonzalez: @sweatje Thanks dude. I am thinking object operator seems to be agreeable.
- RobertGonzalez: Alrighty then. Thanks for everyone’s help on my crazy PHP arrow naming fiasco of ought-nine. Goodnight for now.
More on the operators used in PHP can be found at http://us3.php.net/manual/en/language.operators.php. Unfortunately, these operators will not be found there. So to assist in explaining what these operators are and what they do, lets dig a little deeper in the world of cryptic symbols that remain unnamed in PHP. Shall we?
The double arrow operator, “=>”, is used as an access mechanism for arrays. This means that what is on the left side of it will have a corresponding value of what is on the right side of it in array context. This can be used to set values of any acceptable type into a corresponding index of an array. The index can be associative (string based) or numeric. So if I have an array like:
<?php $myArray = array('Big', 'Small', 'Up', 'Down'); ?> |
Then in effect the code is saying:
<?php $myArray = array( 0 => 'Big', 1 => 'Small', 2 => 'Up', 3 => 'Down' ); ?> |
This is not a real surprise seeing as arrays in PHP are numerically indexed, zero-based arrays by default. But what if you wanted a hash of keys and values? Like when passing something as a form or in a querystring or reading results from a database? Well then, you would want to associate a key to a value in the array, or something like:
<?php $myArray = array( 'Robert' => 'Big', 'Bobby' => 'Smart', 'PHP' => 'Rocks' ); ?> |
Now you have a hash of values assigned to indeces using the double arrow, or array value assignment, operator (“=>”).
The object operator, “->”, is used in object scope to access methods and properties of an object. It’s meaning is to say that what is on the right of the operator is a member of the object instantiated into the variable on the left side of the operator. Instantiated is the key term here. We’ll talk about that a little more in just a bit.
If you come from a background in object oriented programming then you know that every object has a way to access the methods and properties of the instantiated object. In most languages that is the dot operator (“.”). In PHP the dot operator has completely different meaning (it is used to concatenate strings) so don’t use that for object scope resolution. Instead, after instantiating an object, access its methods and properties using the object operator, like this:
<?php $obj = new MyObject(); // Create a new instance of MyObject into $obj $obj->thisProperty = 'Fred'; // Set a property in the $obj object called thisProperty $obj->getProperty(); // Call a method of the $obj object named getProperty ?> |
Now, getting back to object basics, remember I said that instantiated was a key term? The reason for that term is because there is another operator related to scope resolution than can be used but it is neither an arrow nor can it be used on objects. It can only be used on classes (this is as of PHP 5 – since PHP4 has already been fed to the dogs we are only going to talk about PHP 5). Class scope resolution is identified by the Paamayim Nekudotayim, or double colon, operator (“::”).
If you use a static class or static methods or properties within a class, you would not use the object operator (“->”) but would instead use the scope resolution, or double colon, operator, like so:
<?php MyClass::$staticProperty = 'Fred'; // Sets a values into the static class MyClass::getStaticProperty(); // Gets a value from a static method ?> |
Now that you have the power of operators at your fingertips, even if in a very basic sense, you can get busy with assigning and accessing goodies in arrays, objects and classes. And remember as you use these that nothing at all can replace the learning you would get from trying them out, especially when you try to use them the wrong way (error messages can be quite educational).
If I could leave you with one bit of advice it would be to get your hands dirty on some of them and see what the parser tells you when it chokes on one (or more) of them.
And have fun. Stepping in to object oriented programming can be a frustrating, painful experience for some. But it is definitely a rewarding one.
