【发布时间】:2011-03-23 16:23:50
【问题描述】:
我环顾四周,似乎找不到这个问题的答案。
基本上我使用 _call 方法来动态生成 get 和 set 方法,但是在声明变量时 PHP 的默认值是公共的。有没有将一个类中的变量声明为受保护的?
function __call($method, $arguments) {
$prefix = strtolower(substr($method, 0, 3));
$property = strtolower(substr($method, 3));
if (empty($prefix) || empty($property)) {
return;
}
if ($prefix == "get" && isset($this->$property)) {
return $this->$property;
}
if ($prefix == "set") {
$this->$property = $arguments[0];
}
}
【问题讨论】:
-
出于兴趣,你为什么使用 __call 而不是 __get 和 __set?
-
只是看起来更简单,我也从这里复制了它:onlamp.com/pub/a/php/2005/06/16/overloading.html
标签: php oop class visibility