dongrong9938 2013-07-03 02:49
浏览 27
已采纳

php什么时候使用数组作为参数/属性

For the following two classes:

should I use array as a parameter or one by one?

class image {

    public function __construct(array $color, array $padding) {
        ...
    }
}

class image {

    public function __construct($red, $green, $blue, $paddingtop, $paddingright, $paddingbottom, $paddingleft) {
        ...
    }
}

Also for class properties:

class image {
    protected $paddingtop;

    protected $paddingright;

    protected $paddingbottom;

    protected $paddingleft;

    protected $colorred;

    protected $colorgreen;

    protected $colorblue;
}

class image {

    protected $padding = array('top' => ..., 'right' => ..., 'bottom' => ..., 'left' => ...);

    protected $color = array('red' => ..., 'green' => ..., 'blue' => ...);

}

Is it better to treat them as an array or a single variable?

Maybe this question don't have a fixed answer, but I'm just a php beginner and don't have any work experience, I wanna hear your suggestion which way is the most common use or easy for other coders to read.

Thanks!

  • 写回答

2条回答 默认 最新

  • duangao8359 2013-07-03 02:56
    关注

    You don't ever want that many arguments to a function... You could use an options array(s) or you could just use setter methods to set the properties. I have a hard time believing all of those would be required to create an object instance.

    As far as class properties that depends on the nature of their use. Generally speaking seperate properties are better because it makes for an easier to read interface and helps with IDE code hinting. But in this case, especially the padding, i think it would make more sense to use the array like you have in your second example.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址