Java mode overload in PHP

Source: Internet
Author: User
Sometimes, it is very convenient to call the same method of variable parameters in PHP. PHP can define optional parameters with default values to make this practice possible. Brand website construction: http://www.ucantech.com/this method can be used in php5to overload. The following is an example. If you want to use the overload behavior, you need to reload the java method in PHP.
Sometimes it is very convenient to call the same method of variable parameters. PHP can define optional parameters with default values to make this practice possible. Brand Construction: http://www.ucantech.com/

You can use _ call () in php5 to implement this method overload. The following is an example. If you want to overload the behavior, you need to put it in the abstract parent class.

Php code

Abstract class OverloadableObject {
Function _ call ($ name, $ args ){
$ Method = $ name. "_". count ($ args );
If (! Method_exists ($ this, $ method )){
Throw new Exception ("Call to undefined method". get_class ($ this). ": $ method ");
}
Return call_user_func_array (array ($ this, $ method), $ args );

}
}


The main actions of this method are as follows:
Php code

$ Method = $ name. "_". count ($ args );

The display line is defined in the code. If an undefined method is called, the __call () method generates a new method. its name consists of the original method and the number of parameters, separated by underscores. Then call the change method with the newly generated method name and pass the original parameters together.

Now, if you want to use an overload method named multiply, you can use two or three parameters to call it and perform corresponding operations in each case. you can use two methods respectively: multiply_2 and multiply_3

Php code

Class Multiplier extends OverloadableObject {
Function multiply_2 ($ one, $ two ){
Return $ one * $ two;
}
Function multiply_3 ($ one, $ two, $ three ){
Return $ one * $ two * $ three;
}
}

To use this code, only the multiply method with two or three parameters is called.

$ Multi = new Multiplier;

Echo $ multi-> multiply (5, 6). "\ n ";

Echo $ multi-> multiply (5, 6, 7). "\ n"; (fblww-0112)

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.