Analysis of the implementation of PHP hook

Time:2019-10-20

The example of this paper describes the implementation of PHP hook. To share with you for your reference, as follows:

The hook implementation of PHP programming, example explanation and explanation of their functions, 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 work). As long as there is a sample hook, it is easy to copy the first hook to quickly write the second hook. Here is a simple understanding of the hook.

Here is a simple code example:

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

Browser run test output:

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

In a test class, an example method is written. The original method of this example is very simple, that is, to output Hello, but before that, we have other things to do (here I assume that before I input Hello, there is a string to output, and then there is an array to output).

Now we have two ways of writing:

First:Directly implement the functions we need in the method (like the above code)

But there is a problem in this way. 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). In this way, we need to jump into the class test to change every time we change. The development cost is very large, and the code is very difficult to maintain.

Second: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, our implementation is much more convenient. The test class can be simplified into:

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

But now there is still a problem. When we make changes, we still need to make internal changes to the system (if it is a simple array and string, it can be configured, but if it is a complex logic processing, 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 follows certain rules to design).

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 only need to change an external Hello class to control the internal system.
class Hello
{
  public function string()
  {
    $STR = 'I'm a 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();

Operation result:

I’m a hook test
hello

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

As can be seen from the above, a separate class can be formed. After the internal part of the system is fixed, various kinds can be written outside to implement the hook. Now I have written a hello class. If you need to expand a world class, you can also just change the hook instead of changing the test interior. 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 class or world class, inherit this abstract class, and you can write an extension directly.

For more information about PHP, please refer to the following topics: PHP object-oriented programming introduction, PHP array operation skills, PHP basic syntax introduction, PHP operation and operator usage summary, PHP string usage summary, PHP + MySQL database operation introduction and PHP common database operation Skills summary

I hope that this article will be helpful for PHP programming.

Recommended Today

Single and multiple buttons are styled with pictures

I’ve always seen people asking how to style and enlarge the buttons of radio buttons and multi buttons? Let’s share an example I did. 1. First make the button into a picture  2.html page Copy code The code is as follows: <!DOCTYPE HTML> <html> <head> <meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ /> <script type=”text/javascript” src=”jquery-1.10.2.min.js”></script> <script type=”text/javascript”> […]