Parameter Consistency: To Array or Not To Array

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Parameter Consistency: To Array or Not To Array

Post 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).
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Parameter Consistency: To Array or Not To Array

Post by pickle »

I'd say make it require an array - keep it clean.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Parameter Consistency: To Array or Not To Array

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Parameter Consistency: To Array or Not To Array

Post 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
 
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Parameter Consistency: To Array or Not To Array

Post 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
 
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Parameter Consistency: To Array or Not To Array

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Parameter Consistency: To Array or Not To Array

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Parameter Consistency: To Array or Not To Array

Post 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
 
 
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: Parameter Consistency: To Array or Not To Array

Post 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 ;)
Selkirk
Forum Commoner
Posts: 41
Joined: Sat Aug 23, 2003 10:55 am
Location: Michigan

Re: Parameter Consistency: To Array or Not To Array

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Parameter Consistency: To Array or Not To Array

Post 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.
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: Parameter Consistency: To Array or Not To Array

Post 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)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Parameter Consistency: To Array or Not To Array

Post 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
Post Reply