Improvement Tips for passing PHP function parameters _ PHP Tutorial

Source: Internet
Author: User
Tips for Improving the method of passing PHP function parameters. After writing the code for repeated operations, we decided to improve the traditional PHP function parameter passing method and use arrays as parameters. See the following example. first, let's look at a traditional self-built system.After repeated operations, I decided to improve the parameter passing method of the traditional PHP function by using arrays as parameters. See the following example.

First look at a traditional user-defined function

 
 
  1. /**
  2. * @ Purpose: insert a text field
  3. * @ Method Name: addInput ()
  4. * @ Parameter: str $ title form item title
  5. * @ Parameter: str $ name Element name
  6. * @ Parameter: str $ value default value
  7. * @ Parameter: str $ type. the default value is text. password is optional.
  8. * @ Parameter: str $ maxlength maximum input
  9. * @ Parameter: str $ readonly read-only
  10. * @ Parameter: str $ required: required. the default value is false. true indicates required.
  11. * @ Parameter: str $ check form verification function (js) name
  12. * @ Parameter: str $ id element id, which is omitted unless otherwise required.
  13. * @ Parameter: int $ width the width of the element. unit: Pixel.
  14. * @ Parameter: str $ tip element prompt information
  15. * @ Return:
  16. */
  17. Function addInput ($ title, $ name, $Value="", $Type="Text", $Maxlength="255",
    $ Readonly, $Required="False", $ Check, $ id, $ width, $ tip)
  18. {
  19. $ This->Form.="
  20. N";
  21. $ This->Form.="". $ Title .": Label>N ";
  22. $ This->Form.="<InputName ="". $ Name .""Value= "". $ Value .""Type= ""
    . $ Type .""Maxlength= "". $ Maxlength .""Required= "". $ Required .""Check= ""
    . $ Check .""Id= "". $ Id .""Class= "Input" ". $ readonly ."Style= "Width:". $ width.
    "Px ;"ShowName= "". $ Title .""/>";
  23. $ This->Form.="<SpanClass ="Tip">". $ Tip ." Span>N ";
  24. $ This->Form.="
  25. N" ;
  26. }

This is a function that inserts a text box in the form class I wrote.

The call method for passing PHP function parameters is

 
 
  1. $ Form->AddInput ("encoding", "field0", "", "text", 3 ,"");

At the beginning, only $ title, $ name, $ value, $ type, $ maxlength, $ readonly and other parameters are reserved, it is found that these basic parameters cannot meet the requirements. the text box requires JavaScript verification, CSS styles need to be defined, and prompt information needs to be added...

After adding parameters such as $ required, $ check, $ id, $ width, and $ tip, it is found that all previously called functions need to be modified, which adds a lot of work.

PHP function parameter transfer method call method becomes

 
 
  1. $ Form->AddInput ("encoding", "field0", "", "text", 3, "", "true ",""
    , "", 100, "prompt: The number is required. only three digits are allowed ");

If this function is used in many places, it takes a long time to change it one by one.

The following are my improved functions.

 
 
  1. function addInput($a)
  2. {
  3. if(is_array($a))
  4. {
  5. $title = $a['title'];
  6. $name = $a['name'];
  7. $value = $a['value'] ? $a['value'] : "";
  8. $type = $a['type'] ? $a['type'] : "text";
  9. $maxlength = $a['maxlength'] ? $a['maxlength'] : "255";
  10. $readonly = $a['readonly'] ? $a['readonly'] : "";
  11. $required = $a['required'] ? $a['required'] : "false";
  12. $check = $a['check'];
  13. $id = $a['id'];
  14. $width = $a['width'];
  15. $tip = $a['tip'];
  16. }
  17. $title,$name,$value="",$type="text",$maxlength="255",$readonly,$required="false",$check,$id,$width,$tip
  18. $this->form .= "
  19. n";
  20. $this->form .= "".$title.": label>n";
  21. $this->form .= "<input name="".$name."" value="".$value."" type="".$type."" maxlength="".$maxlength."" required="".$required."" check="".$check."" id="".$id."" class="input" ".$readonly." style="width:".$width."px;" showName="".$title."" /> ";
  22. $this->form .= "<span class="tip">".$tip." span>n";
  23. $this->form .= "
  24. n" ;
  25. }

The call method is changed

 
 
  1. $ Form->AddInput (
  2. Array (
  3. 'Title' = "encoding ",
  4. 'Name' = "field0 ",
  5. 'Maxlength' = 3,
  6. 'Required' = "true ",
  7. & Apos; width & apos; = 100,
  8. 'Tip '= "prompt: The number is required. only three digits are allowed ",
  9. )
  10. );

After comparing the passing methods of PHP function parameters, we can find that:

A traditional function requires a large amount of changes when it needs to be extended. when used, it must be written in the order of parameters, which is prone to errors.

New parameters can be added at any time when the improved function is extended. you only need to add the corresponding array key value during the call. each parameter is clear at a glance, so you do not need to consider the order and the code readability is enhanced.

However, there are still some shortcomings in the PHP function parameter passing method. as the amount of code increases, programmers need to write a lot more key values. In addition, the function may affect the efficiency of judgment statements and trielement operation statements.


After repeated operations, I decided to improve the traditional PHP function parameter passing method and use arrays as parameters. please refer to the following example. first look at a traditional self...

Related Article

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.