Is it a bad idea to name parameters like this in PHP?
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).
Anywa开发者_StackOverflow社区y, 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?I think this is a bad idea from the perspective that you don't have any safety if you get the order wrong. Using a map is a much safer way of emulating this. You also have the side effect of setting the values of these variables in the local scope, which may or may not be an issue.
calculateArea( $height = 5, $width = 10 ); # oops!
function calculateArea( $width, $height ) {
// do calculations here...
}
With a array (map), it doesn't matter what order you put them in.
calculateArea( array( 'height' => 5, 'width' => 10 ) ) # yea!
function calculateArea( $dimensions ) {
$width = $dimensions['width'];
$height = $dimensions['height'];
}
With using calculateArea( $width = 10, $height = 5 )
you are not specifying the parameters by their names as you can do in other languages like Python.
Instead $width = 10
and $height = 5
in the function call are just two variable assignment expressions.
The reason for that this still works as expected is that an assignment expression does not just assign the value but it also returns the assigned value:
The value of an assignment expression is the value assigned. That is, the value of "
$a = 3
" is 3.
Thus, effectively calculateArea( $width = 10, $height = 5 )
is calling calculateArea
with the same parameters as calculateArea(10, 5)
does but it also assigns two variables:
calculateArea( $width = 10, $height = 5 );
var_dump($width); // int(10)
var_dump($height); // int(5)
You should not use variable assignments in function calls because it's likely to cause unexpected results. calculateArea($a = 10, $b = 5)
also works. After that function call is completed, $a === 10 and $b === 5. If these variables had different values before the function call, those values are overridden.
You can call your function in any of these ways:
calculateArea(10,5);
or this:
calculateArea($width,$height);
or this:
calculateArea($xvar,5);
But assigning values inside the function call is usually bad practice. It's technically correct, but makes the code hard to read. Instead of doing this:
calculateArea( $width = 10, $height = 5 );
Do this:
$width = 10;
$height = 5;
calculateArea( $width, $height );
Or this:
$width = 10;
$height = 5;
calculateArea( 10, 5 );
They all have the same effect, but declaring the variables outside the function makes it clear what their scope is.
Well, now you know, that PHP hasn't this feature. But I want to say, that passing array is a bad way too. In this way, you have to keep in memory (or in phddoc comment in best case), which keys can be used in array, you can't set 'default value', and you can easy make typo in name of array's key.
So I prefer to use objects for this cases (since PHP hasn't 'structures' too). In object you can set default values, you can set type of needed object in function declaration, and in IDE you can get code-complete for object's fields names.
As example:
class DimensionsParams
{
public $width = 5;
public $height;
}
function calculateArea(DimensionsParams $params)
{
// do calculations here...
return $params->height*$params->width;
}
Of course, it's not best choice for each function, but it's best variant for 'named parameters' (in my opinion).
精彩评论