PHP implementation function reload _ PHP Tutorial

Source: Internet
Author: User
PHP implements function overloading. Originally php? NamePHPPHP does not support function overloading! Php, as a weak type language, cannot directly implement overload like a strong type such as java or c ++. But you can use some php? Name = PHP "> PHP does not support function overloading!

Php, as a weak type language, cannot directly implement overload like a strong type such as java or c ++. However, some methods can be used to indirectly implement overloading.

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

PHP code:

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:

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

Why php? Name = PHP "PHP does not support function overloading! Php, as a weak type language, cannot directly implement overload like a strong type such as java or c ++. However, you can use some...

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.