What is Php 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
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;
?>
__get() magic method
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;
?>
__set() magic method
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;
?>
__isset() magic method
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));
?>
boolean false
__unset() magic method
Example:-
<?php
class student
{
function __unset($variable_name)
{
echo $variable_name.' variabe is not define';
}
}
$stu_obj =new student;
unset($stu_obj->stu_name);
?>