Reputation: 827
I'm making function to insert some data into my database:
function updateInfo($company, $address, $postalnumber, $city, $phone, $email, $ip, $officehours, $etc){}
...and figured out i needed to set most of the parameters optional:
function updateInfo($company=NULL, $address=NULL, $postalnumber=NULL, $city=NULL, $phone=NULL, $email=NULL, $ip=NULL, $officehours=NULL, $etc=NULL){}
..until i figured out the parameter list became too long. So i discovered extract()
and made my parameter list an array of optionals instead:
function updateInfo($vars){
extract($vars);
}
Would you use extract()
this way, or is there better way to pass a lot of optional variables into a function?
Upvotes: 0
Views: 312
Reputation: 827
After a lot of good suggestions, I'm figuring out that actually using the array can be beneficial. So I'm considering this:
updateInfo($vars) {
foreach($vars as $key->value) {
switch ($key) {
case 'phonenumber':
...
break;
case 'etc':
...
break;
default:
...
}
}
}
It leaves me with unlimited arguments, even without knowing about them (default:
) as well as the possibility to be specific with ´case 'specific':`
Upvotes: 0
Reputation: 311
If you really need to use an array because the possible fields that can be modified are so large, then I suggest you refactor your code to inherently work with the array. Here is an example that builds an SQL query as well as modifies an internal state array (yes, it's a contrived example):
<?php
function updateInfo($fields) {
static $state = [
'company' => 'oldCompany',
'address' => 'oldAddress'
];
$setList = [];
foreach ($fields as $key => $value) {
echo("Changing field " . $key . " from " . $state[$key] . " to " . $value . "\n");
$state[$key] = $value;
array_push($setList, $key . " = \"" . $value . "\""); // TODO escape SQL properly
}
$sql = "UPDATE myTable SET " . join(", ", $setList) . " WHERE myRowId = 123456";
echo($sql . "\n");
}
updateInfo([
'company' => 'newCompany',
'address' => 'newAddress'
]);
?>
You can test the above code by saving it to a file such as example.php
file and running php example.php
in a console.
NOTE: you could instead of blindly accepting the keys from the input array, you could examine the key with a switch statement inside of the foreach loop and have more advanced specialized logic per key type.
Upvotes: 1
Reputation: 340
You can also loop through an associated array and assign variables that way based on the key:
$values = array(
'foo'=>'John',
'bar'=>'Smith'
);
foreach($values as $variable=>$value){
$$variable = $value;
}
echo $foo;
echo $bar;
// echos JohnSmith
Upvotes: 0
Reputation: 2088
I found the use of extract() too magic because we don't know from where variables are coming as they are not defined anywhere.
You must consider to use the array as it:
$array['key'];
or
$key = $array['key'];
This is not so bad, and it's very clear for everyone who will read your code.
And i'm okay with the use of an array instead of a lot of parameters where some are optionals. Wordpress is working this way.
For example it has a function that allow you to get posts with some arguments:
$args = array(
'post_type' => 'page',
'posts_per_page' => 'status
);
$pages = get_posts($args);
And there is a lot of other arguments that are optionals that make it easy the use of the function.
You can think of adding an $default array()
in your function with all the default arguments and merge it with the array given in parameters.
Please see this thread as a reference of it : What is so wrong with extract()?
Upvotes: 1