Should my PHP functions accept an array of arguments or should I explicitly request arguments?

If the system is changing so often that using an indexed array is the best solution, I’d say this is the least of your worries. 🙂

In general functions/methods shouldn’t take too many arguments (5 plus or minus 2 being the maximum) and I’d say that you should stick to using named (and ideally type hinted) arguments. (An indexed array of arguments only really makes sense if there’s a large quantity of optional data – a good example being configuration information.)

As @Pekka says, passing an array of arguments is also liable to be a pain to document and therefore for other people/yourself in ‘n’ months to maintain.

Update-ette…

Incidentally, the oft mentioned book Code Complete examines such issues in quite a bit of detail – it’s a great tome which I’d highly recommend.

Leave a Comment