Home > Article > Backend Development > php magic method in-depth understanding of php magic php magic method in php
??Summary: When tracing the source code of the yii framework, I encountered the use of the __set() magic method and was a little confused, so I wrote a demo to test it, and I discovered the mystery, which is summarized as follows.
1. Let’s first take a look at how the magic method __set() is used in the book
??The purpose of using the __set() magic method is to assign values to private properties outside the object and cannot obtain the value of the private property. .
??Function prototype:
void __set(string name,mixed value)
Write a demo:
classCModule {private$_components = '';
publicfunction__set($name,$value){$this->$name = $value;
}
publicfunctionechoPrivate(){echo$this->_components;
}
}
$config = array(
'_components'=>'request'
);
$module = new CModule();
$module->_components = 'request';
$module->echoPrivate();
//打印结果是//request
The above has introduced an in-depth understanding of PHP magic methods, including the content of PHP magic methods. I hope it will be helpful to friends who are interested in PHP tutorials.