新人:为什么不能通过_get()与_set()给private字段赋值并输出?解决思路
Jun 13, 2016 am 10:25 AM
gt
lt
quot
新人:为什么不能通过_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 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

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What are the differences between Huawei GT3 Pro and GT4?

Fix: Snipping tool not working in Windows 11

How to Fix Can't Connect to App Store Error on iPhone

Microsoft is rolling out Windows 11 23H2 build to the release preview channel with Copilot

How to optimize iPad battery life with iPadOS 17.4

Guan Zeyuanjiang Shuying experiences Huawei MateBook GT 14: the perfect combination of technology and art
