Skip to main content

Magic Methods in PHP

Magic methods in php are some predefined function by php compiler which executes on some event. Magic methods starts with prefix __, for example __call, __get, __set. I am including magic methods topic in my oop tutorial here because these method mostly applied in classes of PHP. If you have gone through my previous chapter then you have seen __construct function. __construct is a magic method which automatically call on creating object of the classes. There are verous magic methods in php. Here we will discuss some of the most comman magic methods of php which will be use in object oriented programming. First of let us review all magic method with short description.

 

List of List of Magic Methods in PHP

Magic MethodDescription
__constructThis magic methods is called when someone create object of your class. Usually this is used for creating constructor in php5.
__destructThis magic method is called when object of your class is unset. This is just opposite of __construct.
__getThis method called when your object attempt to read property or variable of the class which is inaccessible or unavailable.
__setThis method called when object of your class attempts to set value of the property which is really inaccessible or unavailable in your class.
__issetThis 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

Above list is the most conman used magic methods in php object oriented programming. Above magic methods of php executes on some specif  events occur on your class object. For example if you simply echo your object then __toString method trigger. Let us create group of related magic method and analyze how it is working.

__construct and __destruct magic method in PHP

__construct method trigger on creation of object. And __destruct triggers of deletion of object. Following is very basic example of __construct and __destruct magic method in php:
class test
{
function __construct()
{
echo 1;
}
function __destruct()
{
echo 2;
}
}
$objT = new test(); //__construct get automatically executed and print 1 on screen
unset($objT);//__destruct triggers and print 2.

__get __set __call and __callStatic 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 method fires when object of your class is trying to call method of property which is either non accessible or not available. 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 a static magic method. It executes when any method of your class is called by static techniques.

Following is example of __get , __set , __call and __callStatic magic methods
class test
{
function __get($name)
{
echo "__get executed with name $name ";
}
function __set($name , $value)
{
echo "__set executed with name $name , value $value";
}
function __call($name , $parameter)
{
$a = print_r($parameter , true); //taking recursive array in string
echo "__call executed with name $name , parameter $a";
}
static function __callStatic($name , $parameter)
{
$a = print_r($parameter , true); //taking recursive array in string
echo "__callStatic executed with name $name , parameter $a";
}
}
$a = new test();
$a->abc = 3;//__set will executed
$app = $a->pqr;//__get will triggerd
$a->getMyName('ankur' , 'techflirt', 'etc');//__call willl be executed
test::xyz('1' , 'qpc' , 'test');//__callstatic will be executed

__isset and __unset magic methods

__isset and __unset magic methods in php are opposite of each other.
__isset magic methods executes when function isset() is applied on property which is not available or not defined. It takes name of the parameter as an argument.
__unset magic method triggers when unset() method is applied on the property which is either not defined or not accessible. It takes name of the parameter as an argument.
Following is example of __isset and __unset magic method 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);

 

Download Code for magic method in PHP.

Above are some of the basic magic methods in the php. For complete list you can go to php.net site.
__construct()__destruct()__call()__callStatic()__get()__set()__isset()__unset()__sleep()__wakeup()__toString()__invoke(),__set_state() and __clone()

Next >> Visibility In PHP

 

Support Me by Sharing This Article

Ankur Kumar Singh

I am a PHP programmer having some knowledge about Linux. I am always interested in web development and knowledge sharing. I am full time tech evangelist part time human being. :-)

34 thoughts on “Magic Methods in PHP

  1. Hi, Nice definations,

    Can you please provide some real programming examples as where may we face the scenario of accessing undefined properties or methods ?

  2. Now You can Create a loginsystem class and after that create a construct for this class with the help[ of using class and just go in code section call a class by creating object and pass username and password that will be practically use

  3. class test
    {
    function __construct()
    {
    echo 1;
    }
    function __destruct()
    {
    echo 2;
    }
    }
    $objT = new test(); //__construct get automatically executed and print 1 on screen
    unset($objT);//__destruct triggers and print 2.

Leave a Reply

Your email address will not be published. Required fields are marked *

shares