2013-02-07 24 views
1

我想設置屬性並在另一個函數上使用它。PHP開關和類屬性問題

while($texts->employees()){ 
     $employee = $employees->get(); 

     switch($employee->getInfoType()){ 

     case 'email': 
      $this->buildemail($employee); 
      break; 
     case 'Name': 
      $this->buildName($employee); 
      break; 
     case 'Numbers': 
      $this->buildNumbers($employee); 
      break; 
    } 

function buildEmail($employee){ 
    $this->email=$employee->getEmail(); //get the email. 
} 

function buildName($employee){ 
    $this->Name=$this->getName(); //get the name 
    $this->employeeInfo=$this->email.$this->name; //combine the email and numbers. 

    //$this->email is '' becasue it's only defined in buildEmail(). 
} 

function buildNumbers($employee){ 
    $this->numbers=$this->getNumbers(); 
} 

我似乎無法得到$this->email在buildName方法,因爲this->emailbuildemail方法定義。 我需要使用開關,因爲每種方法都有很多代碼。有沒有辦法做到這一點?

+1

在調用'$ this-> buildName()'之前,您需要'switch'來調用'$ this-> buildEmail()'。 – nickb

+0

所有這些方法都在同一個類中?你沒有在你的代碼示例中包含類的「包裝器」,這可能會讓你的問題變得混亂 – thaJeztah

回答

0

爲什麼不在buildName方法中調用$employee->getEmail()而不是依靠它在$email

另外:

case 'Name': 
     $this->buildName($employee); 
    case 'Numbers': 
     $this->buildNumbers($employee); 
     break; 

buildName和都將運行,如果$employee->getInfoType()回報 「的名字。」你錯過了兩者之間的break;

0

不能你喜歡:

function buildName($employee){ 
    $this->Name=$this->getName(); //get the name 

    if(null == $this->email) 
     $this->buildEmail($employee); 

    $this->employeeInfo= $this->email.$this->name; //combine the email and numbers. 

    //$this->email is '' becasue it's only defined in buildEmail(). 
} 

我認爲每個員工都必須有一個電子郵件,是正確的?