PHP implementation of function overloading _php tutorial

Source: Internet
Author: User
Was php?name=php ">phpis not supported functionHeavy-duty!

PHP, as a weakly typed language, cannot itself be overloaded like a strong type, such as Java, C + +, as a direct implementation. However, it is possible to implement overloading indirectly by some means.

1. You can use the Func_get_args () and Func_num_args () functions to implement the functions of overloading!!

PHP Code:

  function   rewrite () {  $args  =  Func_get_arg               S   ();   if  ( func_num_args  () = = 1 ) {func1 (  $args  [0<               span>]);   Else   if  ( func_num_args  () = = 2 ) {Func2 (               span> $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 ', ' China '); 
   // 
    Call Func2  
  

2. Use the default values to get the results you want based on the input:

function Test ($name= "Xiao Li",$age= "All") {          echo$name.  $age ;          }        Test ();       Echo "
"; Test ("a"); Echo "
"; Test (

http://www.bkjia.com/PHPjc/777735.html www.bkjia.com true http://www.bkjia.com/PHPjc/777735.html techarticle originally php?name=php "PHP is not support function overloading!" PHP, as a weakly typed language, cannot itself be overloaded like a strong type, such as Java, C + +, as a direct implementation. But you can pass 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.