我看起来很好看,似乎无法找到这个问题的答案.
基本上我使用_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];
}
}
Run Code Online (Sandbox Code Playgroud)