methosHere是调用我创建的类:PHP:呼叫的方法动态
Utils::search($idRole, $this->roles, 'getId');
在的Utils
,搜索方法:
public static function search ($needle, $haystack, $getter) {
$found = false;
$i = 0;
while($i < count($haystack) || $found) {
$object = $haystack[$i];
if($object->$getter === $needle) {
$found = true;
}
}
return $found;
}
草堆是角色对象的数组。下面是角色类的一部分:
class Role
{
private $id;
private $nom;
public function __construct($id = 0, $nom = null) {
$this->id = $id;
$this->nom = $nom;
}
public function getId()
{
return $this->id;
}
}
运行$object->$getter
部分我有一个例外:
Undefined property: Role::$getId
我认为这是动态调用属性的方式..我该怎么办错了?
谢谢
由于'getId'是一种方法,而不是一个道具,你要称呼其为方法:'$ object - > $ getter();' – hindmost 2014-10-03 12:48:52
缺少括号():) – pietro 2014-10-03 12:49:04