Connecting Tech Pros Worldwide Forums | Help | Site Map

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

lawrence k
Guest
 
Posts: n/a
#1: Apr 16 '07
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.


Martin Mandl - m2m tech support
Guest
 
Posts: n/a
#2: Apr 16 '07

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


On Apr 16, 7:19 am, "lawrence k" <lkrub...@geocities.comwrote:
Quote:
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

lawrence k
Guest
 
Posts: n/a
#3: Apr 16 '07

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


On Apr 16, 1:48 am, "Martin Mandl - m2m tech support"
<martin.ma...@gmail.comwrote:
Quote:
On Apr 16, 7:19 am, "lawrence k" <lkrub...@geocities.comwrote:
>
Quote:
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.


Andy Hassall
Guest
 
Posts: n/a
#4: Apr 16 '07

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


On 15 Apr 2007 22:19:57 -0700, "lawrence k" <lkrubner@geocities.comwrote:
Quote:
>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 :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Closed Thread