1

Is it better to pass arguments as an array or as individual arguments to a method? I feel that an array would make the method more flexible to change, although of course this could also be done through inheritance, but makes the data passed a little more opaque, where as variables can be very long winded when dealing with certain types of methods, but more clear to the user of the method?

Which is better and are my assumptions correct?

Examples

public function addToCart($sku, $itemPrice, $itemCost, $itemName, $itemColor .... ) {}

public function addToCart($itemArray, $userArray) {}
5
  • 1
    How are you going to make guarantees about types and validity of values when using arrays? Commented Oct 18, 2017 at 13:02
  • At first glance, it looks like you need to objects: Item and User instead of two arrays. Said that, I would prefer second approach that is more flexible Commented Oct 18, 2017 at 13:02
  • You're possibly better off with Dependency Injection - so you'd pass a specific type of object rather than an array, e.g. addToCart(MyProduct $product) { ... } - that way you've got exactly the data you need in a defined format. Commented Oct 18, 2017 at 13:03
  • PeeHaa - This is the problem I am talking about with opaqueness, also in the case I am currently working on a lot of the data is coming from user sources for that reason I have checks within the method that data is valid. But I have generalised for this question. Commented Oct 18, 2017 at 13:05
  • You are missing my point. Inheritance has nothing to do with any of this. Commented Oct 18, 2017 at 13:06

1 Answer 1

5

The entire point of methods is to make the as descriptive and concrete as possible. If you simply pass an array you have no control over what is sent to the function and what is required by the function to work. This quickly leads to errors and bugs.

If you find yourself in the situation where a method grows too big, you are most likely constructing a function that does too much. A function should do one thing. Learn to split them up and call other functions instead.

Lastly, you should adapt to OOP and pass objects instead of a bunch of separate variables that are not connected in a meaningful way. Instead of this:

public function addToCart($sku, $itemPrice, $itemCost, $itemName, $itemColor .... ) {}

You should do something like this:

public function addToCart(Item $item) {}

Learning how to structure bigger projects, classes and system is a difficult task, and it takes a while to get it right. Even experienced architects and programmers find themselves constructing terrible classes and methods from time to time. Refactoring is key here. Take the time to find the best possible angle to solve a problem, instead of shortcuts such as passing arrays or endless arguments.

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

8 Comments

Assuming $item is a product it's a fairly safe bet that $sku would be an attribute...
@CD001 You're right. I removed it for the sake of simplicity.
Quick question this is PHP can you use static typing in the context you have used? (Item $item)
Although largely I agree, you might need a method somewhere else that accepts an array of items. One does not exclude the other.
@jeroen Of course. There is nothing inherently wrong with passing arrays, but it should not be used as a minified class for a group of variables. That was at least how I understood OP in his question.
|

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.