Magic Methods in PHP
Magic Methods in PHP: We have provided the list of PHP Magic Methods
PHP Magic Methods are the methods which names starts with double underscore, and they will be triggered in response to particular PHP events.
#Example: Suppose we are trying to call a method which is not defined anywhere then in that case one of the special php magic methods named __call() will be automatically called.
- All magic methods MUST be declared as public.
- It is recommended that you do not use function names with __ in PHP unless you want some documented magic functionality.
PHP Magic Methods | Functionality |
---|---|
_construct | This magic methods is called when someone create object of your class. Usually this is used for creating constructor in php5. |
__destruct | This magic method is called when object of your class is unset. This is just opposite of __construct. |
__get | __get() is utilized for reading data from inaccessible properties. |
__set | __set() is run when writing data to inaccessible properties. |
__isset | This magic methods trigger when isset() function is applied on any property of the class which isinaccessible or unavailable. |
__unset | __unset is something opposite of isset method. This method triggers when unset() function called on inaccessible or unavailable property of the class. |
__call | __call magic method trigger when you are attempting to call method or function of the class which is either inaccessible or unavailable. |
__callstatic | __callstatic execture when inaccessible or unavailable method is in static context. |
__sleep | __sleep methods trigger when you are going to serialize your class object. |
__wakeup | __wakeup executes when you are un serializing any class object. |
__toString | __toString executes when you are using echo on your object. |
__invoke | __invoke called when you are using object of your class as function |
You may also like: Core PHP Interview Questions and Answers
__construct method trigger on the creation of an object, and __destruct triggers on deletion of an object.
Following is very basic example of __construct and __destruct magic method in php:
__get, __set, __call and __callStatic all magic methods in php directly related with no accessible method and property of the class.
__get takes one argument and executes when any inaccessible property of the method is called. It takes name of the property as argument.
__set takes two property and executes when object try to set value in inaccessible property. It take first parameter as name of the property and second as the value which object is try to set.
__call() is triggered when invoking inaccessible methods in an object context. It takes 2 parameter First parameter is string and is name of function. Second parameter is an array which is arguments passed in the function.
__callStatic() is triggered when invoking inaccessible methods in a static context.
Syntax:- __isset and __unset magic methods in PHP are opposite of each other.
- __isset magic methods execute when function isset() is applied on the property which is not available or not defined. It takes the name of the parameter as an argument.
- __unset magic method triggers when unset() method is applied to the property which is either not defined or not accessible. It takes the name of the parameter as an argument.
You may also like: OOPS Interview Questions and Answers