为什么当我调用一个不存在的方法时,调用的是 __get 而不是 __call?
Why is __get called instead of __call when I call a nonexistent method?
我有一个 class 看起来像这样:
class MyClass
{
public __get($prop)
{
$method = 'get' . ucfirst($prop);
if (method_exists($this, $metodo))
return $this->$metodo();
if (property_exists($this, $prop))
return $this->$prop;
throw new Exception("Nonexisting property $prop");
}
public function __call($method, $args)
{
$prop = strtr($method, 'add', '');
$prop = lcfirst($prop);
if (is_array($this->$prop))
array_push($this->$prop, $args[0]);
}
}
但是,如果我执行 $obj->addTags('test');
,它会调用 __get, 会给我一个异常消息:
"Nonexisting property addTags"
我应该如何调用我的 __call 而不是我的 __get?
提前致谢!
我刚刚将 strtr 替换为 str_replace。
我没有得到正确的 属性 名称,所以现在可以了。
一旦 Stack Overflow 允许,我会将其标记为已回答。
:-D
我有一个 class 看起来像这样:
class MyClass
{
public __get($prop)
{
$method = 'get' . ucfirst($prop);
if (method_exists($this, $metodo))
return $this->$metodo();
if (property_exists($this, $prop))
return $this->$prop;
throw new Exception("Nonexisting property $prop");
}
public function __call($method, $args)
{
$prop = strtr($method, 'add', '');
$prop = lcfirst($prop);
if (is_array($this->$prop))
array_push($this->$prop, $args[0]);
}
}
但是,如果我执行 $obj->addTags('test');
,它会调用 __get, 会给我一个异常消息:
"Nonexisting property addTags"
我应该如何调用我的 __call 而不是我的 __get?
提前致谢!
我刚刚将 strtr 替换为 str_replace。 我没有得到正确的 属性 名称,所以现在可以了。
一旦 Stack Overflow 允许,我会将其标记为已回答。
:-D