java - 在php、bean中,是否允许使用数组来收集变量?

标签 java php spring

一个经典的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;
    }
}

但是如果我将其简化为:

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

最佳答案

没关系,就像 map 和类的区别一样。

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

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

关于java - 在php、bean中,是否允许使用数组来收集变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44041924/

相关文章:

java - 在 Java 中实现工厂模式的最佳方式

java - 设置 HTTP POST 请求的内容长度

java - 确定按钮不起作用

php - mongodb - 在子数组中搜索,PHP

php - Symfony2+Doctrine2 : How to properly localize records?

PHP 4 变量在范围更改时丢失值

java - 保留 Coda Hale 指标信息

java - 我的代码可以编译但不执行。我哪里做错了?在未来的代码中如何避免这种情况?

java - 尝试查看 map 内容

java - AJAX:对ajax获取的数据进行编码