Php Magic method

What is Php Magic method?

PHP functions that start with a double underscore is called magic method.

Magic methods are always defined inside classes.

Note:- PHP reserves all function names starting with __ as magical. It is recommended that you do not use function names with __ in PHP unless you want some documented magic functionality.

The Magic Method are __construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(), __clone() and __debugInfo()

__construct() method is already define on the constructor page.

__destruct() method is already define on the destructor page.

__call() and __callStatic() method is already define on the Method Overloading Page.

__clone() method is already define on the Object Cloning Page.

__toString() magic method

__toString() method is called whenever you try to use an object as a string context.

Note:- (i) __toString() must return a string value.
(ii) __toString() does not have parameter.
(iii) __toString() can not throw exception, results in fatal error.
(iv) if you call function in the __toString() that can throw exception.

Example:-


<?php
class student {
	public $stu_info = 'student information';
	function __toString() {
		return $this -> stu_info;
	}

}

$stu_obj = new student;
echo $stu_obj;
?>
Output:- student information

__get() magic method

The __get() method is called when code attempts to access a property or variable that is not accessible.

Note:- (i) It accepts one argument, which is the name of the property.
(ii) if variable not defined, then __get() method will be called both inside and outside of class context.

Why we use __get() method?

(i) Without __get() method Program:-
In the below eg. stu_college variable is not define in the class and we are calling this variable through object.
then show error.


<?php
class student {
	public $stu_name = 'John';
	
	function display_name()
	{
	 return $this->stu_name;
	}
}

$stu_obj = new student;
echo $stu_obj->stu_college;
?>

Output:- Undefined property: student::$stu_college

(ii) With __get() method Program:-
In this eg. we use __get() method which return field name which is not define in the class.


<?php
class student {
	public $stu_name = 'John';
	function display_name()
	{
	 return $this->stu_name;
	}
	function __get($field_name) {
		return $field_name;
	}
}

$stu_obj = new student;
echo $stu_obj->stu_college;
?>
Output:- stu_college

__set() magic method

The __set() method is called when code attempts to set a property or variable that is not accessible and property will be public.

Note:- (i) It accepts two arguments, which are the name of the property and the value.

Example:- In this program we set the value of the variable stu_college, which is not define in the class.


<?php
class student {
	public $stu_name = 'John';
	function __get($field_name) {
		return $field_name;
	}

	function __set($field_name, $field_value) {
		$this->$field_name=$field_value.' is my college';
	}
	function display()
	{
	 return $this->stu_college;
	}

}

$stu_obj = new student;
$stu_obj -> stu_college='IIT Delhi';
echo $stu_obj->stu_college;
?>
Output:- IIT Delhi is my college

__isset() magic method

It is invoked when isset() or empty() check non-existent or inaccessible class property.

Note:- __isset() method has only one parameter.

Firstly we read isset() method:-
It is a language construct that checks the initialization of variables or class properties:
eg:- $name=’John’;
var_dump(isset($name)); Output:- true
var_dump(isset($age)); Output:- false

Example of __isset():- In this eg. prop variable is not define in the class when call isset method then __isset() method invoked automatecally.


<?php
class Test
{
    public function __isset($name) {
        echo "Non-existent property '$name'";
    }
}

$obj = new Test;
var_dump(isset($obj->prop));
?>
Output:- Non-existent property ‘prop’
boolean false

__unset() magic method

__unset() is invoked when unset() is used on inaccessible properties. With the help of this method we can check for the undeclared variables in the code. We can also set appropriate error message while testing for variable names getting used in the Class.
Example:-


<?php
class student
{
	function __unset($variable_name)
	{
	echo $variable_name.' variabe is not define';
	}
}
$stu_obj =new student;
unset($stu_obj->stu_name);
?>
Output:- stu_name variabe is not define