Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass variable by reference with func_get_args() in PHP5?

Tags:

php

I currently have a class method/function in this form:

function set_option(&$content,$opt ,$key, $val){

   //...Some checking to ensure the necessary keys exist before the assignment goes here.

   $content['options'][$key][$opt] = $val;

}

Now, I am looking into modification the function a bit to make the first argument optional, allowing me to pass just 3 parameters. In which case, a class property content is used in place of the one I omit.

The first thing that comes to mind is using func_num_args() & func_get_args() in conjunction with this, something like:

function set_option(){

    $args = func_get_args();

    if(func_num_args() == 3){

        $this->set_option($this->content,$args[0],$args[1],$args[2]);

    }else{

       //...Some checking to ensure the necessary keys exist before the assignment goes here.

       $args[0]['options'][$args[1]][$args[2]] = $args[3];

   }

}

How can I specify that I am passing the first argument for this as a reference? (I am using PHP5 so specifying that the variable is passed by reference on function call isn't really one of my better options.)

(I know I can just modify the parameter list so that the last parameter would be optional, doing it like function set_option($opt,$key,$val,&$cont = false), but I'm curious if passing by reference is possible in conjunction with function definitions like above. If it is I'd rather use it.)

like image 331
Bez Hermoso Avatar asked Sep 07 '11 16:09

Bez Hermoso


People also ask

How do you pass a variable in number of arguments in PHP?

PHP supports variable length argument function. It means you can pass 0, 1 or n number of arguments in function. To do so, you need to use 3 ellipses (dots) before the argument name. The 3 dot concept is implemented for variable length argument since PHP 5.6.

How can you pass a variable by reference in PHP?

?> Pass by reference: When variables are passed by reference, use & (ampersand) symbol need to be added before variable argument. For example: function( &$x ). Scope of both global and function variable becomes global as both variables are defined by same reference.

Is PHP pass by value or pass by reference?

Introduction. In PHP, arguments to a function can be passed by value or passed by reference. By default, values of actual arguments are passed by value to formal arguments which become local variables inside the function. Hence, modification to these variables doesn't change value of actual argument variable.

What is Func_get_args?

The func_get_args() function can return an array in which each element is a corresponding member of the current user-defined function's argument list. This function can generate a warning if called from outside of function definition.


1 Answers

Without a parameter list in the function declaration, there's no way to have an argument used as a reference. What you'd need to do is something like

function set_option(&$p1, $p2, $p3, $p4=null){

    if(func_num_args() == 3){
        $this->set_option($this->content,$p1, $p2, $p3);
    }else{
        $p1['options'][$p2][$p3] = $p4;
    }
}

So, depending on the result of func_num_args(), interpret what each parameter really is.

Pretty ugly, and makes for code you wouldn't want to maintain later :)

like image 189
Paul Dixon Avatar answered Oct 12 '22 14:10

Paul Dixon