I'm obviouly confused about setting values using the getter and setter method and would appriciate some advice on where I have gone wrong
isFoo()
or hasFoo()
("is" or "has" methods) are normally used to signify a boolean
result.
I think if you are combining the is
and get
methods together it would lead to confusion.
Try to encapsulate the methods so they perform one task; a possible solution might be.
class MyClass {
protected $identity;
protected $role;
public function getIdentity()
{
if (null == $this->identity) {
$this->identity = $this->zfcUserAuthentication()->getAuthService()->getIdentity();
}
return $this->identity;
}
public function getRole()
{
if (null == $this->role) {
$this->role = $this->getIdentity()->getRole();
}
return $this->role;
}
public function isAdmin()
{
return ('admin' === $this->getRole());
}
public function isVisitor()
{
return ('visitor' === $this->getRole());
}
}
This gives you a clearer API and no need to "set" any roles etc