Use array helper functions to implement unlimited order filling parameters

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.