Home  >  Article  >  Backend Development  >  PHP’s understanding of magic methods

PHP’s understanding of magic methods

coldplay.xixi
coldplay.xixiOriginal
2020-10-07 15:28:331495browse

php’s understanding of magic methods: 1. Automatically set the attribute when the [_set] attribute does not exist; 2. When the [__get] attribute does not exist or cannot be read, set this method to be readable; 3. [ __call】When the method does not exist, it is executed; 4. [__callStatic] When the static method does not exist, it is executed.

PHP’s understanding of magic methods

php's understanding of magic methods:

1, _set: The attribute does not exist Automatically set attributes when

/**
* 属性不存在时通过__set自动设置属性
* @param $key [键名]
* @param $value [属性值]
*/
function __set($key,$value){
$this->arr[$key] = $value;
}
 
代码:
$object->title = 'blue'; //设置不存在的属性,调用__set()
echo $object->title,&#39;<br/>&#39;; //输出不存在的属性,调用__get()
 
输出:
blue

2, __get: When the attribute does not exist or cannot be read, set this method to read

/**
* 属性不存在或不能读取(属性为私有private)时,通过__get读取
* @param $key 键名
* @return 属性
*/
function __get($key){
return $this->arr[$key];
}

3, __call: When the method does not exist, execute

/**
* 方法不存在时,执行__call方法
* @param $func [方法名]
* @param $param [参数]
* @return [description]
*/
function __call($func,$param){
var_dump($func);
echo &#39;<br/>&#39;;
var_dump($param);
echo &#39;<br/>&#39;;
}
 
代码:
$object -> show(&#39;hello&#39;,&#39;world&#39;); //调用不存在的方法,调用__call()
 
输出:
string(4) "show"
array(2) { [0]=> string(5) "hello" [1]=> string(5) "world" }

4, __callStatic: When the static method does not exist, execute

/**
* 静态方法不存在时,执行__callStatic方法
* @param $func [方法名]
* @param $param [参数]
* @return [description]
*/
static function __callStatic($func,$param){
var_dump($func);
echo &#39;<br/>&#39;;
var_dump($param);
echo &#39;<br/>&#39;;
}
 
代码:
IMooc\Object::show(&#39;hello&#39;,&#39;world&#39;); //调用不存在的静态方法,调用__callStatic()
 
输出:
string(4) "show"
array(2) { [0]=> string(5) "hello"  [1]=>string(5) "world" }

5, __toString: When the object is converted to a string, execute

/**
* 当对象转换为字符串时,执行__toString方法
* @return string [description]
*/
function __toString{
return __CLASS__;
}
 
代码:
echo $object,&#39;<br/>&#39;; //将对象以字符串形式输出,调用__toString()
 
输出:
IMooc\Object

6, __invoke: When the object is used as a function, execute

/**
* 当把对象当成函数来使用时,执行__invoke方法
* @param [type] $param [参数]
* @return [type] [description]
*/
function __invoke($param){
var_dump($param);
}
 
代码:
echo $object(&#39;hello&#39;); //将对象当函数使用,调用__invoke()
 
输出:
string(5) "hello"

Related free learning recommendations : php programming(video)

The above is the detailed content of PHP’s understanding of magic methods. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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