|
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.
- Class FileUpload
- {
- Private $ filepath; // specify the path to save the file
- Private $ allowtype = array ("gif", "jpg", "jpeg", "png"); // allowed type
- Private $ maxsize = 1000000; // The size of the file to be uploaded is 1 MB.
- Private $ israndname = true; // whether to rename the file
- Function _ construct ($ filepath, $ allowtype, $ maxsize, $ israndname)
- {
- $ This-> filepath = $ filepath;
- $ This-> allowtype = $ allowtype;
- $ This-> maxsize = $ maxsize;
- $ This-> israndname = $ israndname;
- Var_dump ($ this );
- }
- }
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!
- Class FileUpload
- {
- Private $ filepath;
- Private $ allowtype;
- Private $ maxsize;
- Private $ israndname;
- Function _ construct (
- $ Filepath = "/upload ",
- $ Allowtype = array ("gif", "jpg", "jpeg", "png "),
- $ Maxsize = 1000000,
- $ Israndname = true)
- {
- $ This-> filepath = $ filepath;
- $ This-> allowtype = $ allowtype;
- $ This-> maxsize = $ maxsize;
- $ This-> israndname = $ israndname;
- Var_dump ($ this );
- }
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:
- Class FileUpload
- {
- Private $ filepath;
- Private $ allowtype;
- Private $ maxsize;
- Private $ israndname;
- Function _ construct ($ options = array ([default])
- {
- // Parse the input array
- Foreach ($ options as $ key => $ value ){
- $ This-> $ key = $ value;
- }
- }
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:
- $ Up = new $ FileUpload (array (
- "Filepath" => "/upload ",
- "Israndname" => false,
- & Quot; maxsize & quot; = & quot; 2000000 & quot;
- ));
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:
- Foreach ($ options as $ key => $ value ){
- $ Key = strtolower ($ key );
- ...
- }
By now, our design has met the first three points in the friendliness goal. The overall code is as follows:
- Class FileUpload {
- Private $ filepath;
- Private $ allowtype;
- Private $ maxsize;
- Private $ israndname;
- Function _ construct ($ options = array (
- "$ Filepath" => "./",
- "$ Allowtype" => array ("txt", "jpg "),
- "$ Maxsize" => 1000000,
- "$ Israndname" => true
- )){
- // Parse the input array
- Foreach ($ options as $ key => $ value ){
- $ Key = strtolower ($ key );
- $ This-> $ key = $ value;
- }
- }
- }
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.
- Foreach ($ options as $ key => $ value ){
- $ Key = strtolower ($ key );
- // Determine whether this variable exists in the class
- If (in_array ($ key, get_class_vars (get_class ($ this )))){
- $ This-> $ key = $ value;
- } Else {// error location and parameter prompt
- Echo "Error when init class". get_class ($ this). "with key:". $ key. "in array! ";
- Exit;
- }
- }
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:
- Class FileUpload {
- // The variable names are all in lower case.
- Private $ filepath;
- Private $ allowtype;
- Private $ maxsize;
- Private $ israndname;
- // Assign the default value to the constructor
- Function _ construct ($ options = array (
- "$ Filepath" => "./",
- "$ Allowtype" => array ("txt", "jpg "),
- "$ Maxsize" => 1000000,
- "$ Israndname" => true
- )){
- // Parse the input array
- Foreach ($ options as $ key => $ value ){
- $ Key = strtolower ($ key );
- // Determine whether the key is declared in the class
- If (in_array ($ key, get_class_vars (get_class ($ this )))){
- // Check whether the value meets the requirements
- If (checkValue ($ key, $ value )){
- $ This-> $ key = $ value;
- } Else {
- // The error location and parameters are displayed.
- Echo "Invalid value". $ value. "found when init class". get_class ($ this). "with key:". $ key. "in array! ";
- Exit;
- }
- } Else {
- // The error location and parameters are displayed.
- Echo "Error when init class". get_class ($ this). "with key:". $ key. "in array! ";
- Exit;
- }
- }
- }
- Function checkValue {
- If (){
- ...
- Return true;
- }
- Return false;
- }
- }
|