Introduction
Magic methods in PHP are special methods that are aimed to perform certain tasks. These methods are named with double underscore (__) as prefix. All these function names are reserved and can't be used for any purpose other than associated magical functionality. Magical method in a class must be declared public. These methods act as interceptors that are automatically called when certain conditions are met.
Following magical methods are currently available in PHP
__sleep
public __sleep ( void ) : array
serialize() method in class checks if it has a function name __sleep(). If so, that function is executed prior to any serialization. It is supposed to return an array with the names of all variables of that object that should be serialized.
__wakeup
public __wakeup ( void ) : void
unserialize() method checks there exists a function with the magic name __wakeup(). If present, this function can reconstruct any resources that the object may have.
__serialize
public __serialize ( void ) : array
serialize() method also checks if the class has __serialize() method. If so, it is executed prior to any serialization. It must construct and return an associative array of key/value pairs that represent the serialized form of the object.
__unserialize
public __unserialize ( array $data ) : void
unserialize() also checks for if __unserialize() is present, and it will be passed the restored array that was returned from __serialize(). It may then restore the properties of the object from that array as appropriate
__toString
public __toString ( void ) : string
The __toString() method describes string representation of object. For example, what echo $obj; will print. This method must return a string
__invoke
__invoke ([ $... ] ) : mixed
This method is called when a script tries to call an object as a function.
__set_state
static __set_state ( array $properties ) : object
This static method is called for classes exported by var_export(). It receices one parameter which is an array containing exported properties in the form array('property' => value, ...).
__debugInfo
__debugInfo ( void ) : array
This method is automatically called when var_dump() is executed for dumping an object to get the properties that should be shown. If it isn't defined,all public, protected and private properties will be shown.
__set
public __set ( string $name , mixed $value ) : void
__set() is run when writing data to inaccessible (protected or private) or non-existing properties.
__get
public __get ( string $name ) : mixed
__get() is utilized for reading data from inaccessible (protected or private) or non-existing properties.
__isset
public __isset ( string $name ) : bool
__isset() is triggered by calling isset() or empty() on inaccessible (protected or private) or non-existing properties.
__unset
public __unset ( string $name ) : void
__unset() is invoked when unset() is used on inaccessible (protected or private) or non-existing properties.