PHP magic methods are special methods that are called automatically when certain conditions are met. There are several magic methods in PHP. Every magic method follows certain rules –
- Every magic method starts with a double underscore ( __ ).
- They are predefined and neither can be created nor removed.
- Magic methods have reserved names and their name should not be used for other purposes.
- Magic methods are automatically called when certain criteria are met.
Method Names | Return types | Condition of calling |
__construct() | NaN |
This method gets called automatically every time the object of a particular class is created. The function of this magic method is the same as the constructor in any OOP language. |
__destruct() | NaN |
As the name suggests this method is called when the object is destroyed and no longer in use. Generally at the end of the program and end of the function. |
__call($name,$parameter) | Not mandatory | This method executes when a method is called which is not defined yet. |
__toString() | String |
This method is called when we need to convert the object into a string. For example: echo $obj; The $obj->__toString(); will be called magically. |
__get($name) | NaN | This method is called when an inaccessible variable or non-existing variable is used. |
__set($name , $value) | NaN | This method is called when an inaccessible variable or non-existing variable is written. |
__debugInfo() | array | This magic method is executed when an object is used inside var_dump() for debugging purposes. |
The following are code snippets and examples to understand magic methods better.
__construct() Method: In the below example, the MagicMethod class has a magic method __construct() and it is called every time when a new object of MagicMethod class is created.
PHP
<?php class MagicMethod { function __construct() { echo "This is the construct magic method" ; } } // Creating object of Magic method class $obj = new MagicMethod(); ?> |
This is the construct magic method
__destruct() Method: In the below example, the MagicMethod class has a magic method __destruct() that gets called automatically when the object of MagicMethod destroys.
PHP
<?php class MagicMethod { function __destruct() { echo "This destruct magic method " + "gets called when object destroys" ; } } $obj = new MagicMethod(); ?> |
This destruct magic method gets called when object destroys
__call($name, $parameters) Method: This method gets called when a method or property is called which has not been defined.
This method takes two parameters:
- $name: This contains the name of the method which was called.
- $parameters: This is an array of parameters that were given to that method.
PHP
<?php class MagicMethod { function __call( $name , $parameters ){ echo "Name of method =>" . $name . "\n" ; echo "Parameters provided\n" ; print_r( $parameters ); } } // Instantiating MagicMethod $obj = new MagicMethod(); $obj ->hello( "Magic" , "Method" ); ?> |
Name of method =>hello Parameters provided Array ( [0] => Magic [1] => Method )
__toString() Method: This method gets called when an object is treated as a string. This method is also useful to represent an object as a String.
PHP
<?php class MagicMethod { function __toString(){ return "You are using MagicMethod object as a String " ; } } $obj = new MagicMethod(); echo $obj ; ?> |
You are using MagicMethod object as a String
__get($name) Method: This method gets called when an inaccessible (private or protected ) variable or non-existing variables are used.
PHP
<?php class MagicMethod { function __get( $name ){ echo "You are trying to get '" . $name . "' which is either inaccessible or non existing member"; } } $obj = new MagicMethod(); $obj ->value; ?> |
You are trying to get 'value' which is either inaccessible or non existing member
__set($name, $value) Method: This method is called when an inaccessible variable or non-existing variable is tried to modify or alter.
PHP
<?php class MagicMethod { function __set( $name , $value ) { echo "You are trying to modify '" . $name . "' with '" . $value . "' which is either inaccessible or non-existing member"; } } $obj = new MagicMethod(); $obj ->value = "Hello" ; ?> |
You are trying to modify 'value' with 'Hello' which is either inaccessible or non-existing member
__debugInfo() Method: This method is used when the var_dump() function is called with object as a parameter. This method should return an array containing all the variables which may be useful in debugging.
PHP
<?php class MagicMethod { function __debugInfo(){ return array ( "variable" => "value" ); } } $obj = new MagicMethod(); var_dump( $obj ); ?> |
object(MagicMethod)#1 (1) { ["variable"]=> string(5) "value" }