Example of function overloading functionality implemented by PHP

  • 2021-10-25 06:15:37
  • OfStack

In this paper, an example is given to describe the function overloading function realized by PHP. Share it for your reference, as follows:

Because PHP is a weakly typed language, the input parameter types of functions cannot be determined (type hints can be used, but type hints cannot be used on scalar types such as integers and strings), and for a function, for example, only three input parameters are defined, but PHP enters four or more parameters when running the call. Therefore, based on these two points, it is doomed that functions cannot be overloaded in PHP (similar to Javascript language), and constructors cannot be overloaded.

Since overloading the implementation functions is very helpful to improve the development efficiency, it would be very good if it could be like C # or C + +. In fact, PHP provides a magic method. mixed __call ( string name, array arguments ) . This method is also mentioned in the php manual. According to the official document, this method can realize function overloading. When calling a non-existent method in an object, if the __call() Method, the method is called. For example, the following code:


<?php
class A
{
  function __call ( $name, $arguments )
  {
    echo "__call Call <br/>";
    echo '$name For '.$name."<br/>";
    print_r ($arguments);
  }
}
(new A)->test("test","argument");
?>

The running result is:

__call call
$name is test
Array ( [0] = > test [1] = > argument )

Therefore, function overloading can be realized only by using this magic method.

The code is as follows:


<?php
class A
{
  function __call ($name, $args )
  {
    if($name=='f')
    {
      $i=count($args);
      if (method_exists($this,$f='f'.$i)) {
        call_user_func_array(array($this,$f),$args);
      }
    }
  }
  function f1($a1)
  {
    echo "1 Parameters ".$a1."<br/>";
  }
  function f2($a1,$a2)
  {
    echo "2 Parameters ".$a1.",".$a2."<br/>";
  }
  function f3($a1,$a2,$a3)
  {
     echo "3 Parameters ".$a1.",".$a2.",".$a3."<br/>";
  }
}
(new A)->f('a');
(new A)->f('a','b');
(new A)->f('a','b','c');
?>

The output is as follows:

1 Parameter a
2 Parameters a, b
3 Parameters a, b, c

Similarly, in PHP, the overloading of constructors can only be realized flexibly. The key element of the implementation is to get the input parameters and decide which method to call according to the input parameters. Here is a sample code:


<?php
class A
{
  function __construct()
  {
    echo " Execute constructor <br/>";
    $a = func_get_args(); // Get the parameters in the constructor 
    $i = count($a);
    if (method_exists($this,$f='__construct'.$i)) {
      call_user_func_array(array($this,$f),$a);
    }
  }
  function __construct1($a1)
  {
    echo "1 Parameters ".$a1."<br/>";
  }
  function __construct2($a1,$a2)
  {
    echo "2 Parameters ".$a1.",".$a2."<br/>";
  }
  function __construct3($a1,$a2,$a3)
  {
     echo "3 Parameters ".$a1.",".$a2.",".$a3."<br/>";
  }
}
$o = new A('a');
$o = new A('a','b');
$o = new A('a','b','c');
?>

Execute constructor
1 Parameter a
Execute constructor
2 Parameters a, b
Execute constructor
3 Parameters a, b, c

By the way, like c # and other object-oriented languages, in php, the constructor is set to private or protected You cannot instantiate the object.

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 Skills" and "Summary of php Common Database Operation Skills"

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


Related articles: