Detailed Explanation of Three Methods of Realizing Chain Operation in PHP

  • 2021-08-21 19:46:59
  • OfStack

In this paper, three methods of chain operation in PHP are described by examples. Share it for your reference, as follows:

There are many string functions in php, such as filtering the space at the end of the string first, and then finding its length. The general writing method is:


strlen(trim($str))

What should I write if I want to implement a chain operation similar to that in js, such as the following?


$str->trim()->strlen()

The following are implemented in three ways:

Method 1. Use the magic function __call combined with call_user_func

Idea: Firstly, a string class StringHelper is defined, and the constructor directly assigns value, then the functions trim () and strlen () are called in a chain, and the calling relationship is handled by using call_user_func in the called magic function __call (). The implementation is as follows:


<?php
class StringHelper 
{
  private $value;
  function __construct($value)
  {
    $this->value = $value;
  }
  function __call($function, $args){
    $this->value = call_user_func($function, $this->value, $args[0]);
    return $this;
  }
  function strlen() {
    return strlen($this->value);
  }
}
$str = new StringHelper(" sd f 0");
echo $str->trim('0')->strlen();

Terminal execution script:


php test.php 
8

Method 2. Use the magic function __call in combination with call_user_func_array


<?php
class StringHelper 
{
  private $value;
  function __construct($value)
  {
    $this->value = $value;
  }
  function __call($function, $args){
    array_unshift($args, $this->value);
    $this->value = call_user_func_array($function, $args);
    return $this;
  }
  function strlen() {
    return strlen($this->value);
  }
}
$str = new StringHelper(" sd f 0");
echo $str->trim('0')->strlen();

Description:

array_unshift(array,value1,value2,value3...)

array_unshift() Function is used to insert a new element into an array. The value of the new array will be inserted at the beginning of the array.

call_user_func() And call_user_func_array They are all methods of calling functions dynamically, and the difference lies in the different ways of passing parameters.

Method 3. Do not use the magic function __call to implement

Just need to modify _call() For trim() Function can:


public function trim($t)
{
  $this->value = trim($this->value, $t);
  return $this;
}

The key point is to return the $this pointer, which is convenient for calling the latter function.

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: