skip to Main Content

I’m trying to gather all function arguments within a single variable so I can use the single variable instead of entering all arguments into the function.

Below is what I’ve done so far to try to achieve this and it hasn’t been successful.

Variable that stores all variable values together

$bindargs = "$var1, $var2, $var3"; 

Function which I am trying to pass above variable as 3 separate arguments

function($bindargs); 

I want above to run the same as below:

function($var1, $var2, $var3);

2

Answers


  1. Solution: by using array

    $bindargs = array('var1'=>$var1,'var2'=>$var2,'var3'=>$var3);

    now pass this $bindargs to the function and use like this

    function functionname($bindargs){
    
    $var1 = $bindargs['var1'];
    $var2 = $bindargs['var2'];
    $var3 = $bindargs['var3'];
    // now you can use your logic to perform the action on the variables which you want
    }
    
    Login or Signup to reply.
  2. Well, I suppose you could do something like this, with an array:

    <?php
    
    function func($a, $b, $c) {
        echo $a."n";
        echo $b."n";
        echo $c."n";
    
    }
    
    $bindargs = ['a','b','c'];
    func(...$bindargs);
    
    

    I don’t see why you’d want to. You lose type hinting on your arguments, and named arguments, and this all seems to me like a way just to obfuscate the code and make it difficult for whoever comes after you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search