2017-05-18 46 views
0

經典豆:在php,beans中,是否允許使用數組來收集變量?

class Person { 

    /* Read/Write property*/ 
    private $_firstName; 

    /* Read/Write property*/ 
    private $_lastName; 

    /* Read/Write property*/ 
    private $_birthdate; 

    /* Read/Write property*/ 
    private $_weight; 

    /* Read/Write property*/ 
    private $_height; 

    public function setFirstName($value) { 
     $v = trim($value); 
     $this->_firstName = empty($v) ? null : $v; 
     return $this; 
    } 

    public function getFirstName() { 
     return $this->_firstName; 
    } 

    public function setLastName($value) { 
     $v = trim($value); 
     $this->_lastName = empty($v) ? null : $v; 
     return $this; 
    } 

    public function getLastName() { 
     return $this->_lastName; 
    } 

    /* Read only property */ 
    public function getAge() { 
     /* To be implemented based on birthdate */ 
    } 

    public function setBirthdate(Zend_Date $value) { 
     $this->_birthdate = $value; 
     return $this; 
    } 

    public function getBirthdate() { 
     return $this->_birthdate; 
    } 

    /* Kg */ 
    public function setWeight($value) { 
     $this->_weight = (float) $value; 
     return $this; 
    } 

    public function getWeight() { 
     return $this->_weight; 
    } 

    /* cm */ 
    public function setHeight($value) { 
     $this->_height = (int) $value; 
     return $this; 
    } 

    public function getHeight() { 
     return $this->_height; 
    } 
} 

但如果我會把它簡化爲:

class Person { 

    private $data = []; 

    public function setFirstName($value) { 
     $v = trim($value); 
     $this->data['_firstName'] = empty($v) ? null : $v; 
     return $this; 
    } 

    public function getFirstName() { 
     return $this->data['_firstName']; 
    } 
} 

回答

1

沒問題,就像地圖和班級的不同。

`class object{ 
    private $a, 
    private ¥b 
}` 

object['a'],object['b'] 
相關問題