Page 1 of 1

Parameter Consistency: To Array or Not To Array

Posted: Thu Sep 03, 2009 10:07 am
by Ollie Saunders
I have a function that accepts a parameter. Inside the body of the function the parameter is assumed to be an array of any length. Should I prefix the use of the parameter with an array cast, so that single-element arrays may be specified as scalars when the function is called? Or is this inconsistent leading to calls that appear to be accepting scalars that are in fact being converted into arrays?

In other words, should I do this:

Code: Select all

function commaImplode($pieces) { return implode(', ', $pieces); }
echo commaImplode(array('foo')); # array() required
or this:

Code: Select all

function commaImplode($pieces) { return implode(', ' (array)$pieces); }
# allowing this:
echo commaImplode('foo');
Does the call of the second type seem misleading given that you would expect commaImplode() to accept an array? (Assume that the definition of commaImplode() is hidden away in another file).

Re: Parameter Consistency: To Array or Not To Array

Posted: Thu Sep 03, 2009 10:12 am
by pickle
I'd say make it require an array - keep it clean.

Re: Parameter Consistency: To Array or Not To Array

Posted: Thu Sep 03, 2009 10:32 am
by Ollie Saunders
Looking at some of the other places where I've used an array cast it seems like it's been a good idea when scalar is common, array is occasional, and I want to process all cases as if they were an array, to make the implementation simpler. The kind of thing I'm talking about here, where scalar is occasional, is probably unclean, as you say Pickle. Thanks.

Re: Parameter Consistency: To Array or Not To Array

Posted: Thu Sep 03, 2009 10:35 am
by onion2k
I'd say passing a string to an implode function is a sign that something isn't being done right, so I'd chuck back an error if someone does it.. eg

Code: Select all

function commaImplode($pieces) {
    if (!is_array($pieces)) { return FALSE; } 
    return implode(', ', $pieces);
}
 
echo commaImplode(array('foo')); # returns 'foo'
echo commaImplode('foo'); # returns FALSE
 

Re: Parameter Consistency: To Array or Not To Array

Posted: Thu Sep 03, 2009 10:53 am
by Ollie Saunders
PHP already does that:

Code: Select all

php > var_dump(implode(', ', 'foo'));
 
Warning: implode(): Invalid arguments passed in php shell code on line 1
NULL
 

Re: Parameter Consistency: To Array or Not To Array

Posted: Thu Sep 03, 2009 5:34 pm
by VladSun

Code: Select all

function commaImplode($pieces) {
    if (is_int($pieces)) { return "$pieces"; } 
    elseif (is_string($pieces)) { return $pieces; } 
    elseif (is_array($pieces)) return implode(', ', $pieces);
    ......
}
 
echo commaImplode(array('foo')); # returns 'foo'
echo commaImplode('foo'); # returns 'foo'
So (type)foo is considered a string array of length 1. It's a common approach in ExtJS (for example), where a (config) parameter can be either a string, an object or an array.

Re: Parameter Consistency: To Array or Not To Array

Posted: Thu Sep 03, 2009 6:16 pm
by Benjamin
There are numerous PHP functions which can accept mixed parameter types. I don't see anything wrong with casting/converting, but if the data type used in an argument is clearly incorrect I would trigger an error.

Re: Parameter Consistency: To Array or Not To Array

Posted: Sat Sep 12, 2009 5:51 pm
by josh
whens the last time type casting caught a problem for you? PHP uses recoverable fatal errors for type hinting so your code will still try to run, you will still get an error for using the data wrong which will override the error you see from the type hinting
for ex.

Code: Select all

 
function example( Some_Object $obj )
{
  $obj->foo();
}
 
example( NULL ); // DOESNT THROW TYPE CASTING ERROR
 
 

Re: Parameter Consistency: To Array or Not To Array

Posted: Tue Sep 15, 2009 12:05 am
by PHPHorizons
IMHO Ollie Saunders, since it sounds like you know you might pass a scalar into that function, and since you want is to return a string, I'd say let it do that. There isn't anything "unclean" about allowing mixed types. Even strongly typed languages allow function overloading which essentially gives the ability to handle mixed types.

Anyways, it's all about what is expected to happen. If you expect that a string might be legitimately passed in, then accept it. If not, then make that bad boy throw some kind of nasty Exception ;)

Re: Parameter Consistency: To Array or Not To Array

Posted: Mon Sep 28, 2009 10:24 pm
by Selkirk
I work with a library that makes extensive use of casting strings to arrays. Its not the worst thing, but once you start with the casts, they tend to force you to use more casting because you're not sure what the variables contain.

Re: Parameter Consistency: To Array or Not To Array

Posted: Tue Sep 29, 2009 6:57 am
by josh
PHPHorizons wrote:If not, then make that bad boy throw some kind of nasty Exception ;)
Bugs aren't exceptions ( in general ). It's rarely a good idea to write repetitive code [for every method], you'd have to remove to add functionality later.

Re: Parameter Consistency: To Array or Not To Array

Posted: Tue Sep 29, 2009 6:06 pm
by PHPHorizons
josh wrote:
PHPHorizons wrote:If not, then make that bad boy throw some kind of nasty Exception ;)
Bugs aren't exceptions ( in general ). It's rarely a good idea to write repetitive code [for every method], you'd have to remove to add functionality later.

Who said anything about bugs? Although php really doesn't use exceptions in the most of it's functions, for languages that do, it's entirely acceptable to throw an exception if a function is asked to do something it shouldn't.

Asking a function to do something it shouldn't is not necessarily a bug. For instance, suppose you want to open up a file, but the file doesn't exist, an exception can legitimately be thrown in that case. It doesn't mean it's a bug.

Cheers


(Before someone says I'm wrong, I'd just say, while I respect your opinion, there are languages like c# that does exactly what I outlined)

Re: Parameter Consistency: To Array or Not To Array

Posted: Wed Sep 30, 2009 11:30 am
by josh
Well yeah I'm saying you shouldnt keep repeating stuff like: if ( !is_string( $param1 ) )
better to just allow any paramater, let your string function error out on it's own