For those just getting started with PHP and wondering what these "magic methods" are all about (and they can be a little confusing for someone not used to the language), Lorna Mitchell has a new tutorial posted to the Think Vitamin blog on just that topic.
The "magic" methods are ones with special names, starting with two underscores, which denote methods which will be triggered in response to particular PHP events. That might sound slightly automagical but actually it’s pretty straightforward, we already saw an example of this in the last post, where we used a constructor – so we’ll use this as our first example.
She talks about nine of these magic functions including:
- __get/__set for variable handling
- __sleep/__wakeup for serializing data
- __call/__callStatic for methods in a class
- __construct/__destruct for making and destroying classes