All magic methods of OOP, and practical use case and real world examples.
-
__construct()
-
__destruct()
-
__call($fun, $arg)
: The__call()
method is called when you’re trying to invoke inaccessible methods, the methods that you haven’t defined in your class. -
__callStatic($fun, $arg)
: This method is called when an undefined or inaccessible method is invoked in a static manner. -
__get($property)
: The__get()
method is exactly the opposite of__set()
. The__get()
magic method is called when you try to read data from inaccessible or non-existent object properties. The purpose of this method is to provide values to such properties. -
__set($property, $value)
: The__set()
magic method is called when you try to set data to inaccessible or non-existent object properties. The purpose of this method is to set extra object data for which you haven’t defined object properties explicitly. -
__isset($content)
: The__isset()
magic method is called when you call the isset() method on inaccessible or non-existent object properties. -
__unset($content)
:__unset()
is another magic method that is invoked automatically for destroying a variable and freeing up memory space. When__isset()
is used for checking whether a variable exists or not, the__unset()
simply destroys a variable when used on non-existing or inaccessible properties. -
__sleep()
: This method is called first while executing serialize(). It returns the object’s property array on cleaning PHP class objects before serialization. -
__wakeup()
: The magic method helps to re-establish any connections and start-up tasks when programmers invoke theunserialize()
function with the class object. This method gets invoked automatically when the deserialization takes place. -
__toString()
: The__toString()
magic method allows you to define what you would like to display when an object of the class is treated like a string. If you use echo or print on your object, and you haven’t defined the__toString()
method, it’ll give an error. -
__invoke()
: This method is defined in a class that will be called while trying to call an object in a way of calling function. -
__set_state($array)
: This is a static magic method that is used with thevar_export()
function. As we all know that the var_export() function will provide or return a piece of structured information about any variable or class instance passed within it. When PHP programmers use thevar_export()
function for exporting classes, they use the __set_state() method. -
__clone()
: If you want to duplicate an existing object, you could use the clone keyword to do that. But after cloning, if you want to modify the properties of the cloned object, you can define the__clone()
magic method in your class. -
__debugInfo()
: It is another utility magic method that is called automatically when we want to dump any object with the help of thevar_dump()
function. In case this method is not defined as associating an object, then it dumps all public, protected, and private properties of that class.