In general, we often need to pass arguments to a function when we define a function in a class
1<?PHP2 3 classimage{4 5 Private $name;6 Private $age;7 Private $adress;8 9 functionGetInfo$name,$age,$adress){Ten One $this->name =$name; A $this->age =$age; - $this->adress =$adress; - } the -}
In the process of using the function, we need to enter the parameters strictly in accordance with their order, and can not miss a parameter,
Although the parameter is set to a default value, it is still given a position.
In the learning of Gaulo Teacher's detail in PHP, Gao spoke about the use of arrays to implement the free-form parameter input,
The following steps are implemented:
First, when you define a function, put all the parameters in the array:
Public function getinfo ($options = Array () ) {}
Then you need to assign the parameters in the array to the property values of the class in the function's contents.
When calling a method:
$arr Array (' name ' = ' Timor ', ' age ' =>12, ' adress ' = ' yudel ') { }
When defining a function, assign the parameters that the function receives to the properties of the class:
That is, the argument used by the function must be a property in the class or a variable defined in the script
In the class:
1 Public functionGetInfo$options=Array() ){2 3 foreach($options as $key=$val{//Remove parameters from the array4 if(!In_array($key,Get_class_vars(Get_class($this))) {//Determine if the parameters and properties in the class are the same5 Continue;6}Else{7 $this->$Key=$val; Assigning the value of a parameter to a property in a class8 }9 }Ten}
After that, when we need to call GetInfo (), we can write the following form:
1GetInfo$arr=Array(' name ' = ' Timor ', ' age ' = 12)){2 3 }4 5 //You can also disrupt the order of the parameters6 7GetInfo$arr=Array(' age ' = +, ' name ' = ' Timor ') )){8 9 }Ten One //You can also miss a parameter and not write it . A -GetInfo$arr=Array(' name ' = ' Timor ' )){ - the}
I don't know if this is a bit of a detour ...
It seems to me that this is a good way to implement flexible calling functions, but I don't seem to be optimistic about this approach.
It calls a lot of methods, which will take up some extra memory, and as if doing so will give people a sense of confusion,
Enclosing the parameters in the array seems to complicate the definition of the parameters ...
See practicality later ...
Use array helper functions to implement unlimited order filling parameters