简体   繁体   English

在php中,beans是否允许使用数组来收集变量?

[英]In php, beans, is it allowed to use an array to collect variables?

a classic bean: 经典豆:

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

but what if I would simplify it to: 但是如果我将其简化为:

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

?

That's all right ,just like the difference of the map and the class. 没关系,就像地图和类的区别一样。

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

and

object['a'],object['b']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我们不允许在会话bean中使用静态变量,那么如何定义记录器? - If we are not allowed to use static variables in session beans, how to define a logger? 从数组中收集变量 - Collect variables from in an array 我可以在静态内部类中为片段使用成员变量吗? - am i allowed to use member variables in static inner class for fragments? 不允许属性'profile'出现在元素'beans:bean'中 - Attribute 'profile' is not allowed to appear in element 'beans:bean' EJB3无状态会话Bean是否允许继承? - Is inheritance allowed for EJB3 Stateless Session Beans? Spring config beans - 属性不允许出现在元素中 - Spring config beans - Attribute is not allowed to appear in element 会话Bean中的静态变量限制 - Static variables restriction in session beans Spring Context:是否可以在XML中定义变量(而非属性),并在运行时使用它来获取特定的引用bean? - Spring Context: It's possible to define variables (not properties) in a XML and use it in runtime to obtain specific referenced beans? 如何在Spring XML配置中收集和注入给定类型的所有bean - How to collect and inject all beans of a given type in Spring XML configuration 收集数组中的对象 - Collect Objects in Array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM