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 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)