2014-02-13 1 views
0

약간 OOP 개념과 혼동 스럽습니다. 나는 아래 varable에 대한 값을 설정하려고합니다.사례 방법을 사용하여 PHP OOP

예 :

나는이 값을 설정하는 세터와 게터를 사용하려면
protected $isAdmin; 

; caseMethod를 사용하여 setter에 대한 값을 설정하십시오.

아래에 나와있는 것이 더 쉽습니다. 나는 $this->getIsAdmin()라고하면

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; 
    } 

는 항상 NULL 값을 반환했습니다. 따라서 Case 메서드는 기본적으로 올바른 값을 설정하지 않습니다.

나는 getter와 setter 메서드를 사용하여 값을 설정하는 것에 대해 혼란스러워하고 내가 잘못한 부분에 대한 조언을 제시합니다.

+0

OK 어떤 역할을 "설정"할 필요를 제공하기 때문에 방법은 아마 올바른 값을 설정하지 않습니다. 앞뒤에'$ this-> isAdmin'의 값이 무엇인지보십시오. '$ theRole'의 값을 확인하십시오. 이것은 기본적인 디버깅입니다, 나는 당신이 여기에 어떤 종류의 도움을 줄지 확신하지 못합니다. – Jon

+0

이미 말했듯이'$ theRole'의 값을 확인하십시오. 아마'$ this-> zfcUserAuthentication() -> getAuthService() -> getIdentity() -> getRole()'메소드가 "admin"이나 "visitor"를 반환하지 않을 수도 있습니다. – kinkee

+0

안녕 존과 키키. 모든 기본적인 디버깅 작업을 수행했습니다. 설정되기 전의 값은 null이었고 설정 후의 값은 null이었습니다. 나는 정말로 이것에 대해 조금은 미안하다. 근본적으로. 내 setter 및 getter 메서드가 올바르지 않은 것처럼 보입니다. – andreea115

답변

0

은 내가 getter와 setter 메소드를 사용하여 값을 설정하는 방법에 대한 obviouly 혼란 스러워요 내가 잘못 될 경우에 대한 몇 가지 조언을 appriciate 것

isFoo() 또는 hasFoo() (또는 방법 "가" "있다") 일반적으로 boolean 결과를 나타내는 데 사용됩니다.

isget 방법을 함께 사용하면 혼동을 일으킬 수 있습니다.

메서드를 캡슐화하여 하나의 작업을 수행하도록하십시오. 가능한 해결 방법이있을 수 있습니다.

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()); 
    } 

} 

이 당신에게 명확 API 등

+0

안녕하세요 AlexP님께 추천 해 주셔서 감사합니다. 나는 그것을 지금 시도 할 것이다. 따뜻한 안부 Andreea – andreea115