PHP object-oriented programming simulates an example of method overload in general object-oriented languages

Time:2019-8-12

This paper illustrates the method overload of PHP simulation in general object-oriented language. Share for your reference, as follows:

Method overloading in general object-oriented design languages (such as C++, Java) defines the same method name, and accesses different methods of our same method name through different “number of parameters” or “type of parameters”. But in PHP, methods can not be overloaded, because PHP is a weak type language, so different types of data can be received in the parameters of the method itself, and because the method of PHP can receive an indefinite number of parameters, so it is not possible to call different methods with different method names by passing different numbers of parameters. Vertical. So there is no traditional overload method in PHP, but because of its flexibility, it can simulate the overload method in general object-oriented language.

Simulating method overload in general object-oriented languages in PHP

Let’s start with an example:

<?php
/*
Override means that subclasses override the same name method of the parent class.
Overload: Overload refers to the existence of multiple methods with the same name, but the number of parameters is different. Pass different parameters and call different methods.
However, in PHP, multiple methods with the same name are not allowed. Therefore, this overload in java, c++ cannot be accomplished.
However, PHP is flexible and achieves similar results.
*/
// Method of Simulating Overload in PHP
class Calc {
  Public function area () {// area
    // Determine the number of parameters that a call to area yields
    $args = func_get_args();
    If (count ($args) == 1) {// has only one parameter
      Return 3.14 * $args [0] * $args [0]; // area of circle
    } Other if (count ($args) == 2) {// has two parameters
      Return $args [0] * $args [1]; // Find the area of a rectangle
    } else {
      Return'unknown graphics';
    }
  }
}
$calc = new Calc();
// Calculate the area of a circle
echo $calc->area(10),'<br />';
// Calculate the area of a rectangle
echo $calc->area(5,8);
?>

Operation results:

314
40

Using overloading technology of PHP to realize method overloading in general object-oriented language

The following example is the use of PHP overloading technology, using magic methods to achieve the traditional method overload.

For overloading technology in PHP, please refer to: PHP object-oriented overloading

Example:

<?php
/*
Goal: Design a class, an instance of this class, to achieve the following requirements:
Call method f1:
When one parameter is passed in, it returns itself.
When two parameters are passed in, the sum of squares is obtained.
When three parameters are passed in, the cubic sum is obtained.
Other parameter forms, will report errors!
*/
class A{
  // This is a magic method when the object of A calls a method that does not exist.
  // It will be called automatically to deal with this situation.
  function __call($Func_name, $argument){
    // Non-existent use of F1
    if($Func_name === 'f1'){
      $len = count ($argument); // Number of arguments obtained
      if($len<1 || $len>3){
        Trigger_error ("The number of parameters is wrong! "(E_USER_ERROR);
      }else if($len == 1){
        return $argument[0];
      }else if($len == 2){
        return $argument[0]*$argument[0] + $argument[1]*$argument[1];
      }else if($len == 3){
        $v1 = $argument[0];
        $v2 = $argument[1];
        $v3 = $argument[2];
        return $v1*$v1*$v1 + pow($v2, 3) + pow($v3, 3);
      }
    } Other if ($Func_name==='f2') {// Another method that does not exist
    }else if($Func_name === 'f3'){ //......
    }
  }
}
$a = new A();
$v1 = $a->f1(1);
$v2 = $a->f1(2,3);
$v3 = $a->f1(4,5,6);
echo "v1= $v1, v2 = $v2, v3 = $v3";
?>

The results are as follows:

v1= 1, v2 = 13, v3 = 405

More readers interested in PHP-related content can see the topics of this site: Introduction to Php Object-Oriented Programming, Introduction to PHP Array Operating Skills, Introduction to PHP Basic Grammar, Summary of PHP Operating and Operator Usage, Summary of Php String Usage, and php+mysql Data. Introduction to Library Operations and Summary of Common Database Operating Skills in PHP

I hope this article will be helpful to everyone’s PHP program design.