PHP uses the func_get_args and func_num_args functions to implement function overload instances.

Source: Internet
Author: User
This article mainly introduces how PHP uses func_get_args and func_num_args functions to implement function overloading. The example shows the specific methods for PHP to implement overload, which is a very practical technique. For more information, see

This article mainly introduces how PHP uses func_get_args and func_num_args functions to implement function overloading. The example shows the specific methods for PHP to implement overload, which is a very practical technique. For more information, see

This example describes how PHP uses the func_get_args and func_num_args functions to implement function overloading. Share it with you for your reference. The specific method is analyzed as follows:

Friends who learn php know that php itself has no function overload, not to mention how to use it like java and c ,, however, if you have a deep understanding of the function, you can use the func_get_args () and func_num_args () functions in php to implement function overloading. The following are two examples of function overloading. these two functions implement function overloading.

1. default parameter. If a function is not a required parameter and the corresponding default value is added, the corresponding function can be completed. The Code is as follows:

The Code is as follows:

Function overloadFun ($ param1, $ param2 = '1', $ param3 = true)
{
// Do something
}


Use the func_get_args () and call_user_func_array () functions. The PHP code is as follows:

The Code is as follows:

Function rewrite (){
$ Args = func_get_args ();
If (func_num_args () = 1 ){
Func1 ($ args [0]);
} Else if (func_num_args () = 2 ){
Func2 ($ args [0], $ args [1]);
}
}

Function func1 ($ arg ){
Echo $ arg;
}

Function func2 ($ arg1, $ arg2 ){
Echo $ arg1, '', $ arg2;
}

Rewrite ('php'); // call func1
Rewrite ('php', 'China'); // call func2


2. Use the default value to obtain the desired result based on the input. The Code is as follows:

The Code is as follows:

Function test ($ name = "Xiao Li", $ age = "23 "){
Echo $ name. "". $ age;
}
Test ();
Echo"
";
Test ("");
Echo"
";
Test ("a", "B ");


3. Use the _ call ($ name, $ arg) function for processing. The Code is as follows:

The Code is as follows:

<? Php
Class OverLoad {
Function _ call ($ name, $ args)
{
If ($ name = 'overloadfun ')
{
Switch (count ($ args ))
{
Case 0: $ this-> overloadFun0 (); break;
Case 1: $ this-> overloadFun1 ($ args [0]); break;
Case 2: $ this-> overloadFun2 ($ args [0], $ args [1]); break;
Default: // do something
Break;
}
}
}

Function overloadFun0 ()
{
Echo 0;
}

Function overloadFun1 ($ var1)
{
Echo $ var1;
}

Function overloadFun2 ($ var1, $ var2)
{

Echo $ var1 + $ var2;
}
}
$ Test = new OverLoad ();
$ Test-> overloadFun ()."
".
$ Test-> overloadFun (1 )."
".
$ Test-> overloadFun (1, 2 )."
";
?>


With this method, we can use them to implement simple function overloading, but one thing to note is that php, as a weak type language, cannot be like a strong type such as java, c ++ directly implements heavy load, at least not now. We don't know if it will be available in later versions.

I hope this article will help you with PHP programming.

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.