methosHere is the call to a class I created :
Utils::search($idRole, $this->roles, 'getId');
in the Utils, search method :
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;
}
The haystack is an array of Role objects. Here is a part of the Role class :
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;
}
}
running the $object->$getter part I have an exception :
Undefined property: Role::$getId
I thought that was the way to call a property dynamically.. What do I do wrong ?
thank you
getIdis a method, not a prop, you have to call it as method:$object->$getter();