0

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!

0

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm making a image edit tool, the class has so many properties such as color, padding, border, resource..so I'm wondering what is the code style that people most use, array or single one
@user1970939: Yeah i would definitely use setters/getters for that then. The only thing i would probably have in the consturctor would be for canvas size, color model, and possibly a file location or something. all the other stuff i would use like $image->setPadding($arrayOfValues); or $image->setPadding('top', $value); You might want to take a look at the IMagick api for some inspiration...
1

This would be completely subjective; however, I will throw my two cents in to the conversation. My rule of thumb is if there are more than 4 parameters than try to bundle them arrays if they make sense together. So, the example's padding and colors go together well as arrays.

Also, try to make the arrays in a fashion that the user does not have to worry about which index each element is in, such as using an associative array.

1 Comment

I have an idea, for example, if the properties have the same 'meaning' or 'unit', I will bundle them as an array, such as different padding or rgb for one color, but for fontsize, fontcolor, I will separate them

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.