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
Functions - optional variables, specifying the types of
Moderator: General Moderators
- Pointybeard
- Forum Commoner
- Posts: 71
- Joined: Wed Sep 03, 2003 7:23 pm
- Location: Brisbane, AUS
- Contact:
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
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
- Michael 01
- Forum Commoner
- Posts: 87
- Joined: Wed Feb 04, 2004 12:26 am
Pointybeard, Your suggestion is exactly what is needed.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
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
(
ї0] => tex
ї1] => blah@you.com
ї2]=> 7'2
ї3]=> yes please
)- Pointybeard
- Forum Commoner
- Posts: 71
- Joined: Wed Sep 03, 2003 7:23 pm
- Location: Brisbane, AUS
- Contact:
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:Thanks PB.
*grin* yes. Funny stuff.dr_nailz wrote:P.S. "I'm a banana!"
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.Michael 01 wrote: print_r(array_values($array));
-PB
- Michael 01
- Forum Commoner
- Posts: 87
- Joined: Wed Feb 04, 2004 12:26 am
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.
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.