One man’s voice Thoughts, rants and commentary from a husband, father of five and professional web geek

4Mar/097

PHP operators: double and single arrow

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:

  1. The PHP manual is a very good manual and usually covers things like this; and,
  2. 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:

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.

Comments (7) Trackbacks (0)
  1. Really Helpful. Good article.

  2. A very good article and prolly the only one out there addressing this issue. When i first started with OOP i was having this exact question. Im sure this will be alot of help for new PHP OOP’ers :)

    cheers

  3. Thanks for the kudos. This was an article I had a lot of fun writing.

  4. Great Article. Just getting into this junk now trying to write, or understand a script for authorize.net. This arrow operator was has to find just because I couldn’t figure out what to call it. Thanks for your simplified keywords in URL. Good SEO ;)

  5. I’m glad it was helpful. When I first started programming I saw both of the arrow operators and had no idea what they where. I had asked a question about the operators on the PHP Developer’s Network forum when I was first starting out and got lots of answers. It appears since then there have been several other people who have asked that question over there:

    DISCLAIMER: In the interest of full disclosure… as of now I am a long time administrator of the forums I link to above. When I posted my original question I was a new member. I am not trying to promote the forums, but I will stand by any assertions I may make to the notion that the PHP Developer’s Network forums are indeed one of the best PHP development resources around.

  6. you are wonderful!!!

    what the hell is =& though??? that one confuses me. :(

  7. =& Is setting the variable on the left to a reference to the variable on the right. Have a look at http://us3.php.net/references for more information on references.


Leave a comment


No trackbacks yet.