PHP is a friendly function parameter passing mode design-php Tutorial

Source: Internet
Author: User
PHP is a friendly function parameter passing mode design

PHP is a friendly function parameter passing mode design. if you need it, you can refer to it.


When many parameters need to be input in a constructor of a class, programmers may encounter compilation errors due to the difficult to remember the sequence and writing of passing parameters during encoding, or when the value is passed to the wrong parameter (weak language.

Friendly function design

When a programmer instantiates a class, the input parameters should meet the following friendly goals:
1. only some parameters can be passed in.
2. parameters can be passed in sequence
3. parameter case-insensitive
4. prompt the errors generated when passing parameters in a timely and accurate manner

Improvement Process example 1-to assign a default value to a member variable, see the following code. the default value of the function is assigned to the member variable. in this way, when only some parameters are passed in, the system generates a notice, that is, it does not meet the first in the "friendly target.

  1. Class FileUpload
  2. {
  3. Private $ filepath; // specify the path to save the file
  4. Private $ allowtype = array ("gif", "jpg", "jpeg", "png"); // allowed type
  5. Private $ maxsize = 1000000; // The size of the file to be uploaded is 1 MB.
  6. Private $ israndname = true; // whether to rename the file
  7. Function _ construct ($ filepath, $ allowtype, $ maxsize, $ israndname)
  8. {
  9. $ This-> filepath = $ filepath;
  10. $ This-> allowtype = $ allowtype;
  11. $ This-> maxsize = $ maxsize;
  12. $ This-> israndname = $ israndname;
  13. Var_dump ($ this );
  14. }
  15. }

Example 2-assign a default value to the constructor

This time, in order to eliminate the notice of the previous example, you can pass in any number of parameters. If no parameter is input, use the default value and change the program to: set the default value in the constructor.

Another problem occurs: when the programmer inputs parameters ("/upload", array ("jpg", "gif"), and false), the programmer forgets to input the maxsize parameter, in this case, the system incorrectly assigns false values to maxsize instead of israndname. The system does not output any error, which leaves a hidden danger for future work!

  1. Class FileUpload
  2. {
  3. Private $ filepath;
  4. Private $ allowtype;
  5. Private $ maxsize;
  6. Private $ israndname;
  7. Function _ construct (
  8. $ Filepath = "/upload ",
  9. $ Allowtype = array ("gif", "jpg", "jpeg", "png "),
  10. $ Maxsize = 1000000,
  11. $ Israndname = true)
  12. {
  13. $ This-> filepath = $ filepath;
  14. $ This-> allowtype = $ allowtype;
  15. $ This-> maxsize = $ maxsize;
  16. $ This-> israndname = $ israndname;
  17. Var_dump ($ this );
  18. }

Example 3-encapsulate parameters using arrays

To solve the problem in example 2, we can check the passed values to prevent misplacement of values (normally we will check the passed values ). however, this is not a solution, because the functions we want to implement also enable programmers to pass values in sequence.

To solve this problem, we can use the special characteristics of arrays to encapsulate all transmitted parameters in the array and save them as key => value key-value pairs. see the following code:

  1. Class FileUpload
  2. {
  3. Private $ filepath;
  4. Private $ allowtype;
  5. Private $ maxsize;
  6. Private $ israndname;
  7. Function _ construct ($ options = array ([default])
  8. {
  9. // Parse the input array
  10. Foreach ($ options as $ key => $ value ){
  11. $ This-> $ key = $ value;
  12. }
  13. }

In this way, the order of incoming parameters is solved, and only some parameters are passed.

However, a new problem arises. in this case, the programmer needs to input an array as the parameter, as shown below:

  1. $ Up = new $ FileUpload (array (
  2. "Filepath" => "/upload ",
  3. "Israndname" => false,
  4. & Quot; maxsize & quot; = & quot; 2000000 & quot;
  5. ));

Example 4-solve the case sensitivity of parameters

After using an array to encapsulate parameters, the programmer needs to manually enter the variable name, which leads to writing problems such as MaxSize, maxSize, and maxsize due to different styles, to solve this problem, you only need to declare the member variables in lower case, and then add a line in the constructor:

  1. Foreach ($ options as $ key => $ value ){
  2. $ Key = strtolower ($ key );
  3. ...
  4. }

By now, our design has met the first three points in the friendliness goal. The overall code is as follows:

  1. Class FileUpload {
  2. Private $ filepath;
  3. Private $ allowtype;
  4. Private $ maxsize;
  5. Private $ israndname;
  6. Function _ construct ($ options = array (
  7. "$ Filepath" => "./",
  8. "$ Allowtype" => array ("txt", "jpg "),
  9. "$ Maxsize" => 1000000,
  10. "$ Israndname" => true
  11. )){
  12. // Parse the input array
  13. Foreach ($ options as $ key => $ value ){
  14. $ Key = strtolower ($ key );
  15. $ This-> $ key = $ value;
  16. }
  17. }
  18. }

Example 5: handle input key errors

In the above example, if a programmer passes in a parameter that is not in the class, the system will report an error. here we have two solutions:
1. ignore invalid parameters. only valid parameters are executed.
2. a friendly prompt is displayed, indicating that the input parameter is invalid.

I personally think that for the sake of program robustness, the error code cannot be easily allowed. Therefore, select the second and we will send this error-friendly prompt to the caller.

  1. Foreach ($ options as $ key => $ value ){
  2. $ Key = strtolower ($ key );
  3. // Determine whether this variable exists in the class
  4. If (in_array ($ key, get_class_vars (get_class ($ this )))){
  5. $ This-> $ key = $ value;
  6. } Else {// error location and parameter prompt
  7. Echo "Error when init class". get_class ($ this). "with key:". $ key. "in array! ";
  8. Exit;
  9. }
  10. }

Example 6: Check whether the input value is valid

So far, the expected four goals have been achieved, and in order to make the _ construct () function reusable, you can directly use paste, we abstract the check of value into a function. please refer to the modified code:

  1. Class FileUpload {
  2. // The variable names are all in lower case.
  3. Private $ filepath;
  4. Private $ allowtype;
  5. Private $ maxsize;
  6. Private $ israndname;
  7. // Assign the default value to the constructor
  8. Function _ construct ($ options = array (
  9. "$ Filepath" => "./",
  10. "$ Allowtype" => array ("txt", "jpg "),
  11. "$ Maxsize" => 1000000,
  12. "$ Israndname" => true
  13. )){
  14. // Parse the input array
  15. Foreach ($ options as $ key => $ value ){
  16. $ Key = strtolower ($ key );
  17. // Determine whether the key is declared in the class
  18. If (in_array ($ key, get_class_vars (get_class ($ this )))){
  19. // Check whether the value meets the requirements
  20. If (checkValue ($ key, $ value )){
  21. $ This-> $ key = $ value;
  22. } Else {
  23. // The error location and parameters are displayed.
  24. Echo "Invalid value". $ value. "found when init class". get_class ($ this). "with key:". $ key. "in array! ";
  25. Exit;
  26. }
  27. } Else {
  28. // The error location and parameters are displayed.
  29. Echo "Error when init class". get_class ($ this). "with key:". $ key. "in array! ";
  30. Exit;
  31. }
  32. }
  33. }
  34. Function checkValue {
  35. If (){
  36. ...
  37. Return true;
  38. }
  39. Return false;
  40. }
  41. }

PHP

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.