PHP Object Oriented Programming Simulates Methods in General Object Oriented Languages Overloading of overload Examples

  • 2021-12-12 03:48:37
  • OfStack

In this paper, the method overloading (overload) in PHP simulation 1 general object-oriented language is described as an example. Share it for your reference, as follows:

Method overloading in general object-oriented design languages (such as C + +, Java) is to define the same method name and access different methods with the same method name through different "number of parameters" or different "type of parameters". However, in PHP, methods cannot be overloaded, because PHP is a weakly typed language, so different types of data can be received in the parameters of methods, and because PHP methods can receive an indefinite number of parameters, it is not valid to call different methods with different method names by passing different numbers of parameters. Therefore, there is no traditional overloading method in PHP (overload), but because of its flexibility, php can simulate the overloading method in a general object-oriented language.

Simulating Method Overloading in General Object-Oriented Language in PHP (overload)

Let's start with an example:


<?php
/*
 Rewrite / Covering  override  Refer to : Subclass overrides the parent class's method with the same name 
 Heavy load : overload  Refer to : There are multiple methods with the same name , But the parameter type / Different numbers .  Pass different parameters , Call different methods. 
 But in PHP Medium , Multiple methods with the same name are not allowed.   Therefore , Unable to complete java,c++ This overload in. 
 But ,PHP Flexibility of , Can achieve a similar effect 
*/
// In PHP The method of simulating overload in 
class Calc {
  public function area() {  // Calculate area 
    //  Judge 1 Calls area Hour , Number of parameters obtained 
    $args = func_get_args();
    if(count($args) == 1) {  // Only 1 Parameters 
      return 3.14 * $args[0] * $args[0];  // Find the area of a circle 
    } else if(count($args) == 2) {  // Have 2 Parameters 
      return $args[0] * $args[1];  // Find the area of a rectangle 
    } else {
      return ' Unknown figure ';
    }
  }
}
$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);
?>

Run results:

314
40

The method overloading (overload) in a general object-oriented language is realized by using the overloading (overloading) technology of PHP

The following example uses the PHP overloading (overloading) technology to implement the traditional method overloading (overload) using magic methods.

For overloading techniques in PHP, see: PHP Object Oriented Overloading (overloading)

Examples:


<?php
/*
 Goal: Design 1 Class, an instance of this class, can fulfill the following requirements: 
 Invoke method f1 : 
 Incoming 1 Parameters, it returns itself, 
 Incoming 2 Parameters, just find the sum of its squares, 
 Incoming 3 Parameters, just find their cubic sum, 
 Other parameter forms will report errors! 
*/
class A{
  // This is 1 A magic method, in A When an object of calls a method that does not exist 
  // Will be automatically called to deal with this situation 
  function __call($Func_name, $argument){
    // Use f1 A non-existent situation 
    if($Func_name === 'f1'){
      $len = count($argument); // Get the number of arguments 
      if($len<1 || $len>3){
        trigger_error(" Wrong number of parameters! ", 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);
      }
    }else if($Func_name === 'f2'){ // Another 1 A nonexistent method 
    }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 running result is:

v1= 1, v2 = 13, v3 = 405

For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Grammar", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: