Function overloading in PHP

Source: Internet
Author: User

Because PHP is a weak type language, the input parameter type of the function cannot be determined and the type suggestion can be used, but the type suggestion cannot be used for scalar types such as integer and string ), for a function, for example, only three input parameters are defined, but four or more parameters are input when PHP runs the call. Therefore, based on these two points, PHP is doomed to be unable to overload functions, similar to the Javascript language), nor to overload constructors.

The implementation of function Overloading is very helpful for improving development efficiency. If it can be like C # Or C ++, it would be very good. In fact, PHP provides a magic method, mixed _ call (string name, array arguments ). This method is also mentioned in the php manual. According to the official documentation, this method can implement function overloading. If the _ call () method is defined when a non-existent method is called in an object, the method is called. For example, the following code:

<? Phpclass A {function _ call ($ name, $ arguments) {echo "_ call <br/>"; echo '$ name is '. $ name. "<br/>"; print_r ($ arguments) ;}} (new A)-> test ("test", "argument");?>

The running result is:

_ Call

$ Name is test

Array ([0] => test [1] => argument)

Therefore, you only need to use this magic method to achieve function overloading.

The Code is as follows:

<? Phpclass A {function _ call ($ name, $ args) {if ($ name = 'F') {$ I = count ($ args ); if (method_exists ($ this, $ f = 'F '. $ I) {call_user_func_array (array ($ this, $ f), $ args) ;}} function f1 ($ a1) {echo "1 parameter ". $ a1. "<br/>";} function f2 ($ a1, $ a2) {echo "2 parameters ". $ a1 .",". $ a2. "<br/>";} function f3 ($ a1, $ a2, $ a3) {echo "3 parameters ". $ a1 .",". $ a2 .",". $ a3. "<br/>" ;}} (new A)-> f ('A'); (new a)-> f ('A ', 'B'); (new A)-> f ('A', 'B', 'C' );?>

The output is as follows:

1 parameter
Two Parameters a and B
Parameters a, B, and c

Similarly, in PHP, the implementation of constructor overloading can only be changed. The key factor for implementation is to obtain the input parameters and determine which method to call based on the input parameters. The following is an example code:

<? Phpclass A {function _ construct () {echo "execute the constructor <br/>"; $ a = func_get_args (); // obtain the $ I = count ($ a) parameter in the constructor. if (method_exists ($ this, $ f = '_ construct '. $ I) {call_user_func_array (array ($ this, $ f), $ a) ;}} function _ construct1 ($ a1) {echo "1 parameter ". $ a1. "<br/>";} function _ construct2 ($ a1, $ a2) {echo "2 parameters ". $ a1 .",". $ a2. "<br/>";} function _ construct3 ($ a1, $ a2, $ a3) {echo "3 parameters ". $ a1 .",". $ a2 .",". $ a3. "<br/> "; } $ O = new A ('A'); $ o = new a ('A', 'B'); $ o = new A ('A ', 'B', 'C');?>

Execute the constructor
1 parameter
Execute the constructor
Two Parameters a and B
Execute the constructor
Parameters a, B, and c

By the way, like object-oriented languages such as c #, in php, setting constructor to private or protected makes it impossible to instantiate the object.

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.