PHP Magic Methods
Generally, for each PHP user defined function, it contains two portions, such as, function definition and function call. In some special cases, PHP functions will have function declaration. For example, PHP interfaces functions or PHP abstract functions, that we have seen with abstract access modifiers.
In PHP, we can define some special functions that will be called automatically. Such functions require no function call to execute the code inside these functions. With this special feature, they can be referred as magic functions or magic methods.
For using these magic methods in our PHP project, we need to be known about the following list of points.
- PHP magic methods names are limited with some list of PHP supported keywords, like construct, destruct and etc.
- And, these names are reserved. So, we should not define any function with the name of PHP magic methods.
- PHP magic methods should be started with (__) symbol.
- For defining magic method with (__) followed by different name, apart from the list of PHP supported naming keyword, we need to simulate PHP magic functionality.
- These special functions should be defined by user. But no need to call them explicitly. Rather, it will be called on appropriate event occurrence. For example, class __construct() will be called while instantiating class.
- PHP magic methods must be defined inside the class.
PHP Magic Methods and Purposes
Now, we are going to list all available magic methods in PHP with their purposes. And, let us recollect some of PHP magic methods we have seen already in this blog.
PHP Magic Methods Invoked on Creating Class Instance
Magic Methods with PHP Overloading
The following list of PHP magic methods are used to deal with inaccessible class members created at run time by using PHP overloading concept.
- __get()/__set() – These are magic getters and setters for getting and putting values for class properties created dynamically by PHP property overloading.
- __isset() – This magic method will be invoked automatically while checking whether a required overloaded property is set or not, by using PHP isset() function.
- __unset() – Similarly, when we call PHP unset() function on such dynamically created properties, this magic method will automatically be invoked.
- __call()/__callStatic() – These two magic methods are dedicated for accessing dynamically created but invisible methods on PHP method overloading. These are differed, where __call() will invoke normal PHP overloaded methods, and __callStatic() will invoke static methods
Magic Methods with PHP Object Serialization
The following magics methods will be invoked automatically, while performing serialization with PHP object context. These automatic invocation will be triggered by using a pair of PHP function serialize()/unserialize() used in PHP Object Serialization.
- sleep() – This will be invoked on calling PHP serialize() function. It will return object’s property array on cleaning PHP class objects before serialization.
- wakeup() – It will do the reverse work to restore objects properties and resources on invoking unserialize().
Other Magic Methods in PHP
__toString() – This method is expected return a string value, while using class instances with PHP printing statements. If we specify return values with any other data type, then the following error will occur.
Catchable fatal error: Method TestClass::__toString() must return a string value in...
Without this function, we we print class instance with PHP print and echo construct, then the following error will be displayed.
Catchable fatal error: Object of class TestClass could not be converted to string in...
- __invoke()-This method defined in a class will be called, when we make a function call with the name of that class instance.
- __set_static()-This is a static method invoked while exporting objects property array and expects such array variable as its argument.
- __clone()-This functio is used while PHP Object Cloning.
This PHP code tutorial was published on September 20, 2013.
↑ Back to Top