Page 1 of 1

Functions - optional variables, specifying the types of

Posted: Wed Feb 04, 2004 9:48 pm
by dr_nailz
I'm trying to create a function with several optional variables and several mandatory ones. If possible I would like them in a certain (logical) order. The problem lies in calling the function - how can ensure that certain variables in the call match up with their counterparts in the definition... perhaps by specifying their type? Here's an example:

I would like num1 to be optional, and str1 and str2 to be mandatory.

function callMe ($str1, $num1 = 0, $str2)
{ ... }

...

callMe ("a", "b");

This call doesn't seem to work, I guess because it thinks "b" is for num1, and str2 has been omitted.

I understand this would work by making num1 the last variable, but I need another way to solve this, because my function has 6 variables, 3 optional, and I would like to "mix and match" the optional ones.

Anyone have a solution or workaround?

-nailz

Posted: Wed Feb 04, 2004 10:17 pm
by Pointybeard
Yea, well unfortately thsi is one short falls of using optional vars. The only way, that i know of, todo what you want is function callMe ($str1, $str2, $num1 = 0) What i usually do is say i have

function afunc($man1, $man2, $op1='', $op2='', $op3='')

and if i want to not use op1 and op2 and just give it op3 is to call it with (val, val, '', '', val) Somthing like that. Of course it would need you to know what the function is setting the optional values to.

The other option is to pass it an array of things rather then using the set params. IE function aFunc($param) where $param is something like ("man1' => val, 'man2' => val, '0p3' => val) that way your function can look for the values that are included in param and use what it gets. THis would then require a bit of extra documentation so the user knows what to give the function. Anyway i hope that helps

-PB

Posted: Wed Feb 04, 2004 10:45 pm
by dr_nailz
Thanks PB.

Kinda unfortunate there's no way to do it, or to overload functions - I'd find it really handy =)

P.S. "I'm a banana!"

Posted: Wed Feb 04, 2004 11:13 pm
by Michael 01
Pointybeard wrote:The other option is to pass it an array of things rather then using the set params. IE function aFunc($param) where $param is something like ("man1' => val, 'man2' => val, '0p3' => val) that way your function can look for the values that are included in param and use what it gets. THis would then require a bit of extra documentation so the user knows what to give the function. Anyway i hope that helps

-PB
Pointybeard, Your suggestion is exactly what is needed.

Code: Select all

<?php
$array = array("name" => "tex", "email"=> "blah@you.com", "height"=>"7''2'', "sex"=>"yes please");
print_r(array_values($array));
?> 

The output of this would generate:

Array
(
   &#1111;0] => tex
   &#1111;1] => blah@you.com
   &#1111;2]=>  7'2
   &#1111;3]=>  yes please
)
It works very much like a mysql array call, and you could assign the array numbers to text names instead. Of course some error checking before adding it in the array would be suggested for things like the email address and the height to keep bad parsing symbols out, but other than that..this is the best method by far to get what you are looking for.

Posted: Thu Feb 05, 2004 5:39 am
by Pointybeard
dr_nailz wrote:Thanks PB.
No worries. Overloading would be good...php5 i spose. (Im assuming that will be in it. I havent had a good look)
dr_nailz wrote:P.S. "I'm a banana!"
*grin* yes. Funny stuff.
Michael 01 wrote: print_r(array_values($array));
Hey cool. Didnt know 'array_values' exisited. I use print_r alot to check arrays, always had trouble with large arrays, got hard to read. This will help. heh. Learn something new everyday i guess.

-PB

Posted: Thu Feb 05, 2004 10:11 pm
by Michael 01
I typically did not get into array_values until after a few hard learned lessons and a persistant freind. I was more apt and stubborn headed in the realm of using a few preset functions, and trying to do twice the work for something that ony required half the work..lol..

It makes sense actually if you use DB statments alot. The SQL statements retrieve data and form wide varieties of variables using a ton of different methods to name them, so its only a natural progression to use array_values within a large segment of code to assign or rename.

For myself anyways, print_r is the only way to go. :)