I've been doing a lot of Objective-C programming lately, and now that I'm coming back to PHP, I have to be honest, I miss the named parameters (at first I hated them, now I love them, go figure).
Anyway, in PHP, I sometimes see people passing arrays to functions as a pseudo way of naming parameters (with the added benefit of not having to worry about the order), but sometimes that makes function writing over-complicated. Also, sometimes I want to call the function with named parameters, but sometimes it's easier and faster not to.
Does anyone simply do something like this:
function calculateArea( $width, $height ) {
// do calculations here...
}
// and then call the function like so:
calculateArea( $width = 10, $height = 5 );
It seems to work fine, at least with my version of PHP, and I'm wondering if other people use this technique as a workaround for named parameters as well. I'm also wondering if anyone knows of some compelling reasons why I shouldn't be doing this.
Thanks!
UPDATE:
Thanks for all the quick responses everyone. To make things more clear, let me just say that I understand that passing an associative array is a better option in most scenarios, but sometimes you're working with functions that you've already written and you don't want to change how their called in every single part of your code. Also sometimes you're working with a secondary framework and you don't have any control over how the function was written.
If I'm looking at code I wrote a year ago and I see: echo $zc->expand('Foo', 'Bar', 10, 1, 0, null, null, array('class'=>'code'), false);
, that's not very helpful.
So, to reword my question, I'm basically asking this:
Sometimes passing arguments to a function via assignment is easier to read. What are the downsides of doing this, and will it work in all versions of PHP?