[Distinct] Implementation of method overloading in PhP5

Source: Internet
Author: User

From: http://www.uh80.com /? P = 371

 

 

Method 1: Use the _ call () method

Today, we have encountered a headache: how to implement method overloading (including constructors overloading) in PHP ). after reading a lot of information, direct overloading cannot be achieved. but it can be implemented in disguised form through the _ call () method. in fact, I think this method is very poor. it is the PHP developer who is lazy to come up with such a method, and the degree of overloading implemented by this method is very limited, especially the constructors overload, which is no longer a real constructor. The constructor is automatically executed when an object is created. It is generally not explicitly called. The constructor implemented using the _ call () method must display the call. That is to say, the constructor has the same nature as the common function, but it has different functions.
Below is an example I wrote Code :
<? PHP
Class overloadtest {
Public Function _ call ($ name, $ para ){
If ($ name = 'construct '){
Switch (count ($ para )){
Case 0:
$ This-> cons1 ();
Break;
Case 1:
$ This-> cons2 ($ para [0]);
Break;
Default:
Print "wrong para ";
}
} Else {
Print 'undefined method'. $ name;
}
}
Function cons1 (){
Echo "cons1 () called ";
}
Function cons2 ($ var ){
Echo "cons2 () called .";
}
}
$ Foo = new overloadtest ();
$ Foo-> construct ();
$ Foo-> construct ('test ');
?>

The class overloadtest is defined to enable the class to have two constructors. One is non-parameter, and the other is a parameter (the reload of common methods is exactly the same as this principle ). Since two _ construct () constructors cannot be defined directly, we have to define two common methods: cons1 () and cons2 ($ var), and then define the _ call () method, determine the source of the call in the _ call () method. If you call the construct () function, convert the number of parameters to different methods for execution. If the number of parameters is 0, go to cons1 (). If the number of parameters is 1, go to cons2 (). You can also add the number of supported parameters in the switch-case statement. In addition, if you have enough energy, you can use functions such as is_int () to determine the parameter type, and then you can call different methods based on different parameter types.
The detailed code will not be repeated. If you have any problems, you can discuss them in the group.
---------------
Method 2: Use the func_get_args () and func_num_args () Functions

the func_get_args () and func_num_args () functions have never been used before. Inspired by a friend of phpdream, I studied the functions of these two functions and found that they can effectively solve the problem of parameter transfer during method overloading. Especially when this method is used, the constructor problem is well solved, and you do not need to explicitly call the constructor. This is very good. This method can also be used for normal method overloading.
A complete example is as follows: (taking the constructor as an example)
class overloadtest {
function _ construct () {
$ arr = func_get_args ();
switch (func_num_args ()) {
case 1:
$ this-> cons1 ($ arr [0]);
break;
case 2:
$ this-> cons2 ($ arr [0], $ arr [1]);
break;
}< BR >}< br> function cons1 ($ var) {
echo "cons1 (". $ var. ") called. ";
}< br> function cons2 ($ var1, $ var2) {
echo" cons2 (". $ var1 .",". $ var2. ") called. ";
}< BR >}< br> $ Foo = new overloadtest ('x');
$ foo2 = new overloadtest ('x ', "Y");

?>
According to my experiment, PHP warning may occur in this case because the number of parameters does not match, but I tested in PhP5 + apache2 to open all error_reporting, there is still no warning information. For the sake of insurance, we recommend that you add "@" before the statement before calling the method for overloading to prevent warning information. In this example, you can:
@ $ Foo = new overloadtest ('x ');
@ $ Foo2 = new overloadtest ('x', "y ");
If you call a common method
@ $ Var = Method1 ();
@ $ Var2 = Method1 ('xyz ');

You can modify the code as needed to meet the needs of different parameter numbers.
I personally think the latter method is quite good.

 

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.