小编Leo*_*tin的帖子

在方法中声明受保护的变量

我看起来很好看,似乎无法找到这个问题的答案.

基本上我使用_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)

php oop visibility class

4
推荐指数
2
解决办法
4426
查看次数

标签 统计

class ×1

oop ×1

php ×1

visibility ×1