我正在尝试设置属性并在另一个函数上使用它。
我有
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();
}
我似乎无法在buildName方法中获得$this->email
,因为this->email
是在buildemail
方法中定义的。我需要使用switch,因为每个方法中都有很多代码。有办法做到这一点吗?
为什么不在buildName
方法中调用$employee->getEmail()
,而不依赖于它在$email
中?
还有:
case 'Name':
$this->buildName($employee);
case 'Numbers':
$this->buildNumbers($employee);
break;
如果$employee->getInfoType()
返回"Name",则buildName
和buildNumbers
都将运行。在两者之间缺少一个break;
。
你不能做点什么吗:
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().
}
我假设每个员工都必须有一封电子邮件,对吗?