5 things I learnt reading the PHP Manual

Obscure or otherwise ignored features that I hadn’t come across before.

Like many people, I picked up PHP from various (usually awful) tutorials and the occasional dip into the documentation. I’ve made it a priority to actually read the manuals and specs for the technologies I use. Here’s some of what I learnt reading the PHP Documentation.

  1. You can use [] array notation to access object properties (or anything else) if you implement the ArrayAccess Interface. The only actual use of this I can think of is for switching to objects instead of arrays but still supporting unmodified scripts. It’d be easier to use the second thing I learnt, though:
  2. PHP can do great type casting. Casting is essentially forcing a variable to assume another type, and takes the following syntax:

    $string = (string) $object;    # forces the object to become a string

    The most useful use of this is to dynamically switch between objects and arrays. If you have some code that might be sent either, just cast the questionable variable to your favourite and use that syntax.

  3. Any variable can be stored as a bytestream representation with serialize() and resurrected with unserialize() (fellow Brits, watch out for the spelling here!).
  4. PHP supports a whole load of fake protocols (wrappers), including ones beginning with php:// which give you access to some input and output streams
  5. If you make a mistake involving the scope resolution operator (double colon, e.g. ::), you may well get an error message with some Hebrew in it.

The Security Section is particularly useful and informative — if you’re only going to read one bit of the manual, make it this one.

It was cool to see a couple of xkcd comics included too.