魔法get方法有问题

时间:2011-02-20 12:12:43

标签: php

我正在尝试在php中编写自己的魔法__get方法。 但是我收到了这个错误: __get()必须正好使用1个参数。 错误是指下面函数中的最后一行。

有什么想法吗?

public function __get()
{
    $method = 'get'.ucfirst($name);
    if(!method_exists($this, $method))
            throw new Exception('Invalid property');
    var_dump($method);
    return $this->{$method}();

}

6 个答案:

答案 0 :(得分:4)

将第一行更改为

public function __get($name) {

http://no.php.net/__get

答案 1 :(得分:2)

传递$name

public function __get($name)

答案 2 :(得分:2)

这样更好吗?

public function __get($name)
{
    $method = 'get'.ucfirst($name);
    if(!method_exists($this, $method))
            throw new Exception('Invalid property');
    var_dump($method);
    return $this->{$method}();

}

我添加了名字参数。

答案 3 :(得分:2)

魔法get函数只需要一个参数,就像错误说的那样。请尝试以下代码:

public function __get( $name )

答案 4 :(得分:1)

您必须指定一个参数,该参数引用您尝试访问的变量名称。

答案 5 :(得分:1)

__get($ name)函数中缺少一个参数。 这将为您提供所需变量的值。

public function __get($name) {
...
...
}
相关问题