469,927 Members | 1,803 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

can one get the name of parameters passed to a function?

One thing I like about Ruby is the use of symbols when passing
function parameters to a function. One thing I dislike about PHP is
that if a function has 4 optional parameters but you want to do
something with the 4th one, you need to put in something, perhaps
empty strings, for the first 3 parameters, even though you've no
interest in using them.

I'm wondering if there is anyway to emulate the Ruby style with
something like this:

http://us2.php.net/manual/en/function.func-get-args.php

Is there a way to get the parameters as an associative array? A
numerically indexed array is just as useless as the normal style of
passing arguments.

Apr 16 '07 #1
3 1772
On Apr 16, 7:19 am, "lawrence k" <lkrub...@geocities.comwrote:
One thing I like about Ruby is the use of symbols when passing
function parameters to a function. One thing I dislike about PHP is
that if a function has 4 optional parameters but you want to do
something with the 4th one, you need to put in something, perhaps
empty strings, for the first 3 parameters, even though you've no
interest in using them.
Dear Lawrence,

try to modify your function from

function dummy($param1 = '', $param2 = '', $param3 = '', $param4 =
'') {
if ($param1) echo $param1;
if ($param2) echo $param2;
if ($param3) echo $param3;
if ($param4) echo $param4;
}

to

function dummy(&$params) {
if (isset($params['first'])) echo $params['first'];
if (isset($params['second'])) echo $params['second'];
if (isset($params['third'])) echo $params['third'];
if (isset($params['forth'])) echo $params['forth'];
}
This way you can use the function e.g. with the fourth parameter only
when you call

dummy(array('forth' ='testvalue'));
Good luck
Martin
------------------------------------------------
online accounting on bash bases
Online Einnahmen-Ausgaben-Rechnung
http://www.ea-geier.at
------------------------------------------------
m2m server software gmbh
http://www.m2m.at

Apr 16 '07 #2
On Apr 16, 1:48 am, "Martin Mandl - m2m tech support"
<martin.ma...@gmail.comwrote:
On Apr 16, 7:19 am, "lawrence k" <lkrub...@geocities.comwrote:
One thing I like about Ruby is the use of symbols when passing
function parameters to a function. One thing I dislike about PHP is
that if a function has 4 optional parameters but you want to do
something with the 4th one, you need to put in something, perhaps
empty strings, for the first 3 parameters, even though you've no
interest in using them.

Dear Lawrence,

try to modify your function from

function dummy($param1 = '', $param2 = '', $param3 = '', $param4 =
'') {
if ($param1) echo $param1;
if ($param2) echo $param2;
if ($param3) echo $param3;
if ($param4) echo $param4;
}

to

function dummy(&$params) {
if (isset($params['first'])) echo $params['first'];
if (isset($params['second'])) echo $params['second'];
if (isset($params['third'])) echo $params['third'];
if (isset($params['forth'])) echo $params['forth'];
}
I should just always remember to pass an associative array instead of
a list of arguments? Well, okay, that is kind of obvious. The syntax
is slightly cleaner in Ruby, but sure, we can all do that in PHP too.
Apr 16 '07 #3
On 15 Apr 2007 22:19:57 -0700, "lawrence k" <lk******@geocities.comwrote:
>One thing I like about Ruby is the use of symbols when passing
function parameters to a function. One thing I dislike about PHP is
that if a function has 4 optional parameters but you want to do
something with the 4th one, you need to put in something, perhaps
empty strings, for the first 3 parameters, even though you've no
interest in using them.

I'm wondering if there is anyway to emulate the Ruby style with
something like this:

http://us2.php.net/manual/en/function.func-get-args.php

Is there a way to get the parameters as an associative array? A
numerically indexed array is just as useless as the normal style of
passing arguments.
For an insane solution, search Google Groups for "This calls for the power of
Bobo the Clown" (seriously).

Otherwise, pass an associative array.
--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Apr 16 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by John Miles | last post: by
11 posts views Thread by Andrew Thompson | last post: by
6 posts views Thread by dharmadam | last post: by
3 posts views Thread by Matthew Keene | last post: by
19 posts views Thread by JRough | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.