我對這個OOP概念有點困惑。我正在嘗試爲下面的可用性設置一個值。使用案例方法的PHP OOP
即:
protected $isAdmin;
我想用一個setter和getter設置這個值;並使用caseMethod來設置setter的值。
下面可能會更容易展示您;
protected $isAdmin = null;
public function isAdminWorker()
{
//this will get the preset values for the user; i.e admin worker or visitor.
$theRole = $this->zfcUserAuthentication()->getAuthService()->getIdentity()->getRole();
switch($theRole)
{
case 'admin':
$this->setIsAdmin($theRole);
break;
case 'visitor':
$this->setVisitor($theRole);
break;
}
}
public function setIsAdmin($isAdmin)
{
$this->isAdmin = $isAdmin;
}
public function getIsAdmin()
{
return $this->isAdmin;
}
當我叫$this->getIsAdmin()
它總是返回NULL
值。所以,Case方法基本上沒有設置正確的值。
我很迷惑關於使用getter和setter方法設置值,並會在我出錯的地方給出一些建議。
OK,所以這種方法很可能無法設置正確的值。看看'$ this-> isAdmin'在前後的價值。檢查'$ theRole'的值。這是基本的調試,我不知道你期望在這裏有什麼樣的幫助。 – Jon
就像已經說過的那樣,檢查'$ theRole'的值。可能你的方法'$ this-> zfcUserAuthentication() - > getAuthService() - > getIdentity() - > getRole()'不返回「admin」或「visitor」。 – kinkee
hi jon and kinkee。我已經完成了所有基本的調試。設置之前的值爲空,設置後的值爲空。我對這一個確實有些不解。 bascially。它看起來像我的二傳手和getter方法不正確 – andreea115