Home >> Latest Articles >> Magic Methods in PHP

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.

Important points: PHP Magic Methods
  • 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 and __destruct - PHP Magic methods

__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 - PHP Magic methods

__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:
public mixed __call ( string $name , array $arguments )
public static mixed __callStatic ( string $name , array $arguments )
#Example of __get , __set , __call and __callStatic - php magic methods
class MethodTest
{
    public function __call($name, $arguments)
    {
        // Note: value of $name is case sensitive.
        echo "Calling object method '$name' " . implode(', ', $arguments). "\n";
    }
    /** As of PHP 5.3.0 */
    public static function __callStatic($name, $arguments)
    {
        // Note: value of $name is case sensitive.
        echo "Calling static method '$name' " . implode(', ', $arguments). "\n";
    }
}
$obj = new MethodTest;
echo $obj->runTest('in object context');
echo MethodTest::runTest('in static context'); // As of PHP 5.3.0

//Output: 
Calling object method 'runTest' in object context
Calling static method 'runTest' in static context
__isset and __unset - PHP Magic methods
  1. __isset and __unset magic methods in PHP are opposite of each other.
  2. __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.
  3. __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
#Example of __isset and __unset magic methods in PHP
class test
{
    function __isset($name)
    {
        echo "__isset is called for $name";
    }
    function __unset($name)
    {
        echo "__unset is called for $name";
    }
}
$a = new test(); isset($a->x); unset($a->c);
For more details on Magic Methods visit http://php.net/manual/en/language.oop5.magic.php
#Example of __toString magic methods in PHP
class Person{
    function getName(){ return "Ramesh"; }
    function getAge(){ return 24; }
    function __toString(){
        return $this->getName(). "'s age is ". $this->getAge();
    }
}
$person = new Person();
echo $person;

//Output: Ramesh's age is 24
Full Stack Tutorials

Author | Blogger @FullStackTutorials | View all articles