PHP magic methods
I can’t ever recall the magic methods of PHP and sometimes google doesn’t seem to display them as the first couple of hits, so for my own sake, a cheat-sheet of them with a few notes:
- __construct(/* arguments */) : called on construction of the object
- __destruct() : called when the object is destroyed
- __get($var) : called when the object does NOT contain the property $var or when $var is private or protected. ALSO called on empty($object->$var) if $var is private or protected
- __set($var, $value) : called when the object does NOT contain the property $var or when $var is private or protected.
- __call($method, $args) : called when the object does NOT contain the method $method. Unlike __get and __set it doesn’t hide private or protected methods.
- __callStatic($method, $args) : equivalent to __call but for static calls. New as of 5.3.
- __clone() : called on clone object.
- __isset($var): called when empty() or isset() is called on a non-existing or private or protected property.
- __unset($var) : called on unset when object does NOT contain $var or $var is private or protected.
- __sleep() : called if serialize() is called on the object. Should return array of properties that will be serialized before the object is destroyed. Allows object to do cleanup before serialization.
- __wakeup() : called if unserialize() is called on the object. Notifies the object that it is being brought back to life.
- __toString() : let’s a class decide what to return if it is converted to string.
- __invoke(/* arguments */) : called if the objectt is called as a function. New as of 5.3
- __set_state($array) : static method called for classes exported by var_export.
And, the manual page describing the methods.
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.