新人:为什么不能通过_get()与_set()给private字段赋值并输出?
例子:
class Employee {
private $name;
function _get($propName) {
echo "_get called!
"; // 并且不能输出任何字串
return $this->$propName;
}
function _set($propName, $propValue) {
$this->$propName = $propValue;
}
}
$employee = new Employee();
$employee->name = "Mario";
echo $employee->name."
";
?>
为什么会这样子?是不是要开启什么参数?
------解决方案--------------------function _get($propName) {
echo "_get called!
"; // 并且不能输出任何字串
return $this->$propName;
}
标红的不是只有一个下划线吗?
实现这个功能的魔术函数名是 __get
是两个下划线
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