Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Passing arrays to increase function parameter readability

0 views
Skip to first unread message

joecommodore

unread,
Dec 26, 2009, 10:47:21 AM12/26/09
to
How does this sound?

instead of using the standard method of passing mixed data to
functions, where you have to track back to the function to see what
the data structure is...

$result = myFunction( array( $id, array('nolog', 'blank'), $data);

Instead pass an array with the elements labeled:

$result = myFunction( array(
'id' => $id
,'options'=> array('nolog', 'blank')
,'data' => $data
));

Any opinions?

Jerry Stuckle

unread,
Dec 26, 2009, 9:15:42 PM12/26/09
to

I guess it depends on what you need. I don't see anything wrong with
it, but am struggling to come up with a real-world reason to use it over
a standard parameter list (with default values, if necessary). But then
I don't often pass mixed data to functions. I find it has a tendency to
overly-complicate the code, and I always code for clarity first.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstu...@attglobal.net
==================

C. (http://symcbean.blogspot.com/)

unread,
Dec 27, 2009, 6:59:05 PM12/27/09
to

Yes there's lots out there. And several languages which use named
parameters. Have google.

C.

Danny Wilkerson

unread,
Dec 29, 2009, 2:18:51 AM12/29/09
to
very interesting, but not as fun/unnecessary as sending an object /
smiles

$result = myFunction((object)array(
'id' => $id,


'options'=> array('nolog', 'blank'),
'data' => $data
));

$test = (object)array(
'id' => 'hi',


'options'=> array('nolog', 'blank'),

'data' => 'stuff'
);

echo $test->hi;

Danny Wilkerson

unread,
Dec 29, 2009, 2:21:43 AM12/29/09
to
echo $test->id; //lol oops

Danny Wilkerson

unread,
Dec 29, 2009, 2:21:51 AM12/29/09
to
0 new messages