新人:为什么不能通过_get()与_set()给private字段赋值并输出?解决思路

WBOY
Release: 2016-06-13 10:25:56
Original
1010 people have browsed it

新人:为什么不能通过_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
是两个下划线
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