Analysis of Implementation Method of PHP Hook

  • 2021-12-11 07:06:38
  • OfStack

This paper describes the implementation method of PHP hook with examples. Share it for your reference, as follows:

PHP programming hook implementation, examples to explain and explain their role, write a template hook implementation

Hook is a common concept in programming, which is very important. It makes the system very easy to expand (without understanding its internal implementation mechanism, which can reduce a lot of workload). As long as there is a hook sample, it is easy to imitate the first hook to write the second hook quickly. Here, a simple understanding of the hook is carried out.

Here is one of the simplest code examples:


<?php
class Test
{
  public static function example()
  {
    $arr = array(1,2,3,4,5,6);
    echo ' I am 1 Hook test <br>';
    echo 'hello<br/>';
    echo '<pre>';
    print_r($arr);
    echo '</pre>';
  }
}
Test::example();

Browser runs test output:

I am a hook test
hello
Array
(
[0] = > 1
[1] = > 2
[2] = > 3
[3] = > 4
[4] = > 5
[5] = > 6
)

An example method is written in an Test class. The original method of example is very simple, that is, to output hello, but before that, we have other things to do (here I assume that before entering hello, there is a string to output, and after that, there is an array to output).

We now have two ways to write it:

Number 1: Implement the functionality we need directly in the method (as in the code above)

However, there is a problem with this method, that is, every time we change the system, we need to change the core part of the system (we assume that Test is the core part of the system), which will require us to jump to the class Test every time we change, and the development cost is very high, and the code is very difficult to maintain in one case.

Number 2: We encapsulate an execute method


function execute($params)
{
  if(is_array($params)){
    echo '<pre>';
    print_r($params);
    echo '</pre>';
  }else{
    echo $params;
  }
}

In this way, when we implement it, it is much more convenient, and the Test class can be simplified as:


class Test
{
  public static function example()
  {
    execute(' I am 1 Hook test <br>');
    echo 'hello<br/>';
    $arr = array(1,2,3,4,5,6);
    execute($arr);
  }
}

However, there is still a problem. When we change it, we still have to change it inside the system (if it is a simple array and string, it can be configured, but if it is a complex logical processing, the configuration will not work).

We want to write a class (through this class, when sending messages to the system, the system can call our class directly, and our class is compatible with the original system as long as it is designed according to a certain rule).

The following hook format has been improved and designed:


<?php
/**
 *  Hook class 
 */
class Hook
{
  static public function execute($type, $model='')
  {
    if($model == ''){
      $m = new Hello();
    }else{
      $m = new $model();
    }
    if($type == 'string'){
      $m->string();
    }elseif($type == 'arr'){
      $m->arr();
    }
  }
}
class Test
{
  public static function example()
  {
    Hook::execute('string');
    echo 'hello<br/>';
    Hook::execute('arr');
  }
}
// We just have to change 1 External Hello Class , The internal control of the system can be realized 
class Hello
{
  public function string()
  {
    $str = ' I am 1 Hook test <br>';
    echo $str;
  }
  public function arr()
  {
    $arr = array(1,2,3,4,5,6);
    echo '<pre>';
    print_r($arr);
    echo '</pre>';
  }
}
Test::example();

Run results:

I am a hook test
hello

Array
(
[0] = > 1
[1] = > 2
[2] = > 3
[3] = > 4
[4] = > 5
[5] = > 6
)

It can be seen from the above that a separate class is formed. After the interior of the system is fixed, various classes can be written externally to realize hooks. Now I have written an Hello class. If I need to expand an World class, I can only change Hook without changing the interior of Test, as long as we define an abstract class:


abstract class lan
{
  abstract function string();
  abstract function arr();
}

Then let all extension classes, such as Hello or World, inherit this abstract class, and you can write an extension directly.

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 Syntax", "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 paper is helpful to everyone's PHP programming.


Related articles: