palmaceous
palmaceous

Reputation: 41

Referencing a parameter in a function directly

I'm trying to edit a piece of code written by a development company, which uses the following construct a lot:

$dbcol = grabdata($strSqlB,'','','','','','',2);

Is there really not an easier way to do this? The code is completely unreadable.

I would have thought that the following would work, and work well for readability:

$vars = array("parameter1" => $strSqlB, "parameter7" => 2);
$dbcol = grabdata($vars);

is there anything that needs to be refactored in the function itself to make this work? Is there anything else clever we could do to make this less of a clusterfudge?

Upvotes: 1

Views: 79

Answers (3)

jd_7
jd_7

Reputation: 1862

Rebuild your params for accept null.

function grabdata($strSqlB , $param7 , $param1 = null , $param2 = null , ....){
   //Code
}

//Exec
$dbcol = grabdata($strSqlB , $param7);

Upvotes: 1

Daniel Ingraham
Daniel Ingraham

Reputation: 560

Refactoring would be necessary as the function would need to reference the parameter array by the appropriate key, rather than by specific parameter variable references. Regarding what you could do to further refactor, I'd have to know more about the function itself.

Upvotes: 0

ITroubs
ITroubs

Reputation: 11215

you will have to refactor this part:

function  grabdata($parameter1, $parameter2,$parameter3,$parameter4,$parameter5,$parameter6,$parameter7){

to

function  grabdata($vars){
extract($vars);

these two snippets should do exactly the same IF $vars is like you described in your question

Upvotes: 1

Related Questions