Home > Backend Development > PHP Tutorial > Introduction to how to implement overloading in php

Introduction to how to implement overloading in php

黄舟
Release: 2023-03-16 17:34:01
Original
1677 people have browsed it

This article mainly introduces the common methods of overloading in PHP. It analyzes four common implementation techniques of overloading in PHP by comparing it with Java in the form of examples. Friends in need can refer to the following

Examples of this article Common methods for implementing overloading in PHP. Share it with everyone for your reference, the details are as follows:

php is a weakly typed language and does not have overloading like a strongly typed language like JAVA.

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: (compared with java 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);
  }
}
Copy after login

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
?>
Copy after login

2. Optional parameters, allowing variables to set default values

JAVA overload 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);
  }
}
Copy after login

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
?>
Copy after login

You can see the same function, but it is easier to implement in a weakly typed language like php

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;);
//看起来似乎通过一个接口调用
//但其实内部已经调用了各自定义的方法
?>
Copy after login

4. Use the magic method __call()

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

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

$nameThe parameter is the name of the method to be called$argumentThe 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");
?>
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template