Home  >  Article  >  Backend Development  >  What is the method to implement overloading in php

What is the method to implement overloading in php

coldplay.xixi
coldplay.xixiOriginal
2020-11-04 14:06:403643browse

How to implement overloading in php: 1. Weak type, no need to define variable types; 2. Optional parameters, allowing variables to set default values; 3. Variable parameters; 4. Use the magic method [__call() 】.

What is the method to implement overloading in php

How to implement overloading in php:

php is a weakly typed language and is not as strong as JAVA Type languages ​​also have overloading.

Overloading generally means having the same function name or method name, but different parameter lists (including the number of parameters and parameter types)

From this definition, PHP does not Overloaded, because PHP does not allow the same function name to exist.

But not having it doesn’t mean it can’t be achieved.

php has four mechanisms to achieve overloading: (java is used for comparison below)

1. Weak type, no need to define variable types

Let’s give an example of JAVA overloading:

class demo
{
  public static void main (String[] args)
  {
    sum(1,1);//2
    sum(1,1.5);//2.5
    sum(1.5,1.5);//3.0
  }
  public static void sum(int var1,int var2){
      System.out.println(var1+var2);
  }
  public static void sum(int var1,double var2){
      System.out.println(var1+var2);
  }
  public static void sum(double var1,double var2){
      System.out.println(var1+var2);
  }
}

If the above code is implemented in PHP language:

<?php
function sum($var1,$var2){
  echo $var1+$var2;
}
sum(1,1);//2
sum(1,1.5);//2.5
sum(1.5,1.5);//3
?>

2. Optional parameters, allowing variables to set default values

JAVA overloading example:

class demo
{
  public static void main (String[] args)
  {
    sum(1,2);//3
    sum(1,2,3);//6
  }
  public static void sum(int var1,int var2){
      System.out.println(var1+var2);
  }
  public static void sum(int var1,int var2,int var3){
      System.out.println(var1+var2+var3);
  }
}

Use PHP optional parameter feature to implement:

<?php
function sum($var1,$var2,$var3=NULL){//$var3设置默认值
  echo $var1+$var2+$var3;
}
sum(1,2);//2
sum(1,2,3);//2.5
?>

You can see the same function, but PHP, a weakly typed language, is simpler to implement

3. Variable parameters

Mainly use the following two functions:

  • func_num_args()Return the number of parameters of the function

  • func_get_args()Return an array containing the function parameter list

Specific Implementation method:

<?php
class demo{
  public function method(){
    $numargs = func_num_args();//获得参数数量
    $args = func_get_args();//获得参数数组
    switch ($numargs) {//通过变量个数来确定调用哪个方法
      case 2:
        $this->method2($args[0],$args[1]);
        break;
      case 3:
        $this->method3($args[0],$args[1],$args[2]);
        break;
    }
  }
  private function method2($var1,$var2){
    #code...
  }
  private function method3($var1,$var2,$var3){
    #code...
  }
}
//调用
$obj = new demo();
$obj->method(&#39;var1&#39;,&#39;var2&#39;);
$obj->method(&#39;var1&#39;,&#39;var2&#39;,&#39;var3&#39;);
//看起来似乎通过一个接口调用
//但其实内部已经调用了各自定义的方法
?>

4. Use magic method __call()

public mixed __call ( string $name , array $arguments )

When calling an inaccessible method in an object, __call() will be automatically called

$name parameter is the name of the method to be called$ The argument parameter is the parameter array of the called method

The specific implementation method is similar to the third point above:

<?php
class demo{
  public function __call($name,$arguments){
    if($name == "method"){
      switch (count($arguments)) {//获得参数数组个数
        case 2:
          $this->method2($args[0],$args[1]);
          break;
        case 3:
          $this->method3($args[0],$args[1],$args[2]);
          break;
      }
    }
  }
  private function method2($var1,$var2){
    #code...
  }
  private function method3($var1,$var2,$var3){
    #code...
  }
}
//调用
$obj = new demo();
$obj->method("var1","var2");
$obj->method("var1","var2","var3");
?>

Related free learning recommendations: php programming (video )

The above is the detailed content of What is the method to implement overloading in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn