Home > Backend Development > PHP Tutorial > Detailed explanation of php magic method, detailed explanation of php magic_PHP tutorial

Detailed explanation of php magic method, detailed explanation of php magic_PHP tutorial

WBOY
Release: 2016-07-13 10:14:25
Original
765 people have browsed it

Detailed explanation of php magic method, detailed explanation of php magic

From PHP 5 and later, classes in PHP can use magic methods. It stipulates that methods starting with two underscores (__) are reserved as magic methods, so it is recommended that function names do not start with __ unless it is to overload an existing magic method. PHP reserves all class methods starting with _ _ (two underscores) as magic methods.

__toString() and __invoke()

public string __toString (void): This method will be automatically called when the object is used as a string. This method must return a string

Copy code The code is as follows:

class Magic{
Public function __tostring(){
             return "hello world!";
}
}
$obj = new Magic();
echo $obj;//hello world!
?>

__invoke(): This method will be automatically called when the object is called as a method.

Copy code The code is as follows:

class Magic{
Public function __tostring(){
             return "hello world!";
}
         public function __invoke($x){
                  echo "__invoke called with param ".$x."n";
}
}
$obj = new Magic();
$obj(10);//__invoke called with param 10
?>

__call() and __callStatic()

__call(): When the object accesses a method name that does not exist, the __call() method will be automatically called

__callStatic(): When the object accesses a non-existent static method name, the __callStatic() method will be automatically called

Through these two methods, calls with the same method name can be implemented corresponding to different methods

Copy code The code is as follows:

class Magic{
//The `$name` parameter is the name of the method to be called. `$arguments` parameter is an enumeration array,
//Contains the parameters to be passed to the method `$name`.
         public function __call($name,$arguments){
                      //implode() function combines array elements into a string. implode(separator,array)
                  echo "Calling " . $name ." with param: ".implode(", ",$arguments)."n";
}
}
$obj = new Magic();
$obj->run("para1","para2");//obj calls the run method, output: Calling run with param: para1, para2
?>

__get() and __set()

When assigning values ​​to inaccessible properties, __set() will be called
When reading the value of an inaccessible attribute, __get() will be called

Copy code The code is as follows:

class Magic{
//There must be static keyword before function
Public function __get($name){
           return "Getting the property " . $name;
}
}
$obj = new Magic();
echo $obj->className."n";//Getting the property className
?>

When reading the value of an inaccessible attribute, __get() will be called

Copy code The code is as follows:

class Magic{

public function __set($name,$value){
echo "Setting the property " . $name ."to value ". $value ."n";
}
}
$obj = new Magic();
$obj->className = 'MagicClass';//Setting the property classNameto value MagicClass
?>

__isset() and __unset()

When isset() or empty() is called on an inaccessible property, __isset() will be called
When unset() is called on an inaccessible property, __unset() will be called

Copy code The code is as follows:

class Magic{
Public function __isset($name){
           echo "__isset invokedn";
        return true;
}
}
$obj = new Magic();
echo '$obj->className is set?'.isset($obj->className)."n";//__isset invoked $obj->className is set?1
?>

The above is the introduction and examples of 8 PHP object-oriented magic methods. I hope it will be helpful to everyone

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/909341.htmlTechArticlephp magic method detailed explanation, php magic detailed explanation From PHP 5 and later versions, classes in PHP can use magic methods . It stipulates that methods starting with two underscores (__) are reserved as magic methods...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template