我已经很好看,并且似乎无法找到此问题的答案。在方法中声明受保护的变量
基本上我使用_call方法来动态生成get和set方法,但是当声明一个变量时PHP的默认值是public的。无论如何,从类内部声明一个变量为受保护的?
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? – 2010-07-31 17:08:23
只是似乎更容易,我也从这里复制它: http://onlamp.com/pub/a/php/2005/06/16/overloading.html – 2010-07-31 17:09:43