【问题标题】:defining member variables inside an array operation在数组操作中定义成员变量
【发布时间】:2012-06-01 04:20:40
【问题描述】:

我想知道在我稍后在其他地方使用的数组中声明我的成员变量是否是错误的约定(在下面的代码中,我传递给 insertArray() 函数)。这是我的代码:

class myClass{

    private $ID;
    private $name;
    private $type;
    private $catID;
    private $isDefault;
    private $isRequired;

    function __construct($compID = 0, $catID = 0, $name = '', $type = '', $default = false, $required = false){

    $myArray = array(
        'name'       => ($this->name = $name),
        'type'       => ($this->type = $type),
        'catID'      => ($this->catID  = $catID),
        'isDefault'  => ($this->isDefault = $default),
        'isRequired' => ($this->required  = $required)
    )
    self::insertArray($myArray);
   }
 }

在 $myArray 中定义我的成员变量是不是很糟糕?我很确定在 php 中,形式 ($this->value = $value) 的语法会将 $value 存储在 $this->value 中,然后只计算为 ($this->value)。

【问题讨论】:

  • 您拥有的代码应该可以正常工作。

标签: php oop class-variables member-variables


【解决方案1】:

它是否不好取决于您遵循的编码指南。他们中的大多数人每行只允许一个操作,但你有两个。

PSR-2 standard 为例。

它也不那么容易阅读,因为它并不常见。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-04
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多