__call() and __callStatic() are called when somebody is calling nonexistent object method in
object or static context.
__clone is invoked by use of the clone keyword. It is used to manipulate object state upon cloning, after the object has been actually cloned.
__construct() and __destruct()
__construct() is the most common magic method in PHP, because it is used to set up a class when it is initialized. The opposite of the __construct() method is the __destruct() method. This method is called when there are no more references to an object that you created or when you force its deletion. PHP's garbage collection will clean up the object by first calling its destructor and then removing it from memory.
This method is called by var_dump() when dumping an object to get the properties that should be shown. If the method isn't defined on an object, then all public, protected and private properties will be shown. — PHP Manual
The above example will output:
The above example will output:
__get(), __set(), __isset() and __unset()
Whenever you attempt to retrieve a certain field from a class like so:
PHP invokes the magic method __get($name), with $name equal to "height" in this case. Writing to a class field like so:
Will invoke the magic method __set($name, $value), with $name equal to "height" and $value equal to 10.
PHP also has two built-in functions isset(), which check if a variable exists, and unset(), which destroys a variable. Checking whether a objects field is set like so:
Will invoke the __isset($name) function on that object. Destroying a variable like so:
Will invoke the __unset($name) function on that object.
Normally, when you don't define these methods on your class, PHP just retrieves the field as it is stored in your class. However, you can override these methods to create classes that can hold data like an array, but are usable like an object:
empty() function and magic methods
Note that calling empty() on a class attribute will invoke __isset() because as the PHP manual states:
empty() is essentially the concise equivalent to !isset($var) || $var == false
This magic method is called when user tries to invoke object as a function. Possible use cases may include
some approaches like functional programming or some callbacks.
__sleep() and __wakeup()
__sleep and __wakeup are methods that are related to the serialization process. serialize function
checks if a class has a __sleep method. If so, it will be executed before any serialization.
__sleep is supposed to return an array of the names of all variables of an object that should be
__wakeup in turn will be executed by unserialize if it is present in class. It's intention is to re-establish
resources and other things that are needed to be initialized upon unserialization.
Whenever an object is treated as a string, the __toString() method is called. This method should return a string representation of the class.
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0