Get the App
SLTechnology News&Howtos  ›  Development  › 

PHP uses func_get_args () and func_num_args () functions to realize function overloading.

Shulou Source: shulou.com Published: 2022-06-03 15:47:20 09月22日 Update

This article mainly explains "PHP uses func_get_args() and func_num_args() functions to implement overloading of functions", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. PHP uses func_get_args() and func_num_args() functions to overload functions.

1. You can use func_get_args() and func_num_args() to overload functions!!

PHP code:

The copy 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 '); //invoke func1

rewrite ('PHP ',' China'); //invoke func2

2. Use default values to get the desired result based on input:

The copy code is as follows:

function test($name="Xiao Li",$age="23"){

echo $name. " ".$ age;

}

test();

echo "";

test("a");

echo "";

test("a","b");

At this point, I believe that everyone has a deeper understanding of "PHP uses func_get_args() and func_num_args() functions to implement overloading of functions", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Tags: Function code content learning practical deeper two interest practicality practical simple operation method more friends results website channel Xiao Li query input Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Linux Huawei vpn MariaDB