PHP using Func_get_args and Func_num_args functions to implement a function overload instance _php technique

Source: Internet
Author: User

This article illustrates the method of using Func_get_args and Func_num_args functions to implement function overloading in PHP. Share to everyone for your reference. The specific methods are analyzed as follows:

Learning PHP's friends know that PHP itself is not a function overload this said, not to mention the use of the method like Java,c, but if we dig deeper we will find that you can use the Func_get_args () and Func_num_args () functions in PHP to implement the function overload, Here are two examples of function overloads. These two functions implement the overloads of the functions.

1, the default parameters, if a function inside, this is not required parameters, and add the corresponding default value, you can complete the corresponding functions, the code is as follows:

Copy Code code as follows:
function Overloadfun ($param 1, $param 2 = ' 1 ', $param 3 = True)
{
Do something
}

Using Functions Func_get_args () and Call_user_func_array (), the PHP code is as follows:
Copy Code code 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 ($arg 1, $arg 2) {
echo $arg 1, ', $arg 2;
}

Rewrite (' PHP '); Call Func1
Rewrite (' PHP ', ' it '); Call Func2

2, use the default value, so that according to input, get their desired results, the code is as follows:
Copy Code code as follows:
function Test ($name = "Xiao Li", $age = "23") {
echo $name. "". $age;
}
Test ();
echo "<br/>";
Test ("a");
echo "<br/>";
Test ("A", "B");

3, the use of __call ($name, $arg) function for processing, the code is as follows:
Copy Code code as follows:
<?php
Class overload{
function __call ($name, $args)
{
if ($name = = ' Overloadfun ')
{
Switch (count ($args))
{
Case 0: $this->overloadfun0 ();
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 ($var 1)
{
echo $var 1;
}

function overloadFun2 ($var 1, $var 2)
{

echo $var 1+ $var 2;
}
}
$test =new overload ();
$test->overloadfun (). " <br/> ".
$test->overloadfun (1). " <br/> ".
$test->overloadfun (1,2). " <br/> ";
?>


With this method we can use them to implement simple function overload, but one thing to note is that PHP as a weak type language, itself can not be as strong as the type like java,c++, the direct implementation of overloading, at least now can not later version will have we do not know.

I hope this article will help you with your PHP program design.

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.