The so-called Hook mechanism is a popular technology developed from Windows programming. The main idea is to bury a hook in a place where functionality can be added in advance. This hook has no practical meaning, and when we need to modify or add logic to this place, we can simply mount the extended class or method to this point.

When learning the Hook mechanism, the author refers to the TAG and Hook mechanism of TP3.2.3, and uses the MVC framework developed by himself. Its directory style and so on all imitate TP3.2.3, which makes the big guys laugh.

For a simple example, we are now going to write a user registration function, as shown below:

/App/Home/Controller/IndexController.class.php





One day, a customer said I needed to add a feature that would reward new users with 50 points for signing up, so I wrote:





The client is satisfied, but another colleague needs your code. You tell him that you pulled from Git. ╬ if you want to send an email to the user after registration, you will do this:





What do you do if you have a large number of similar changes in a project? Just change it? Projects just get bigger and bigger and more cluttered. Stabbing a client to death? Hey, beat the law (▔, ▔)ㄏ. Resign? When I think about my mortgage and my wife and children, I can’t. · ω´ ·). Ps: Where do programmers have wives and children… ( ̄∀ ̄)

Anyway, the best solution is to use the hook mechanism.

Let’s start with the Hook class we wrote:

/CutePHP/Lib/Cute/Hook.class.php





This is a simple Hook class modeled after TP’s Hook, which contains a static private Hooks property that keeps track of all registered Hooks

The add method passes in the name and method of the hook to store it in an array, and listen listens for a hook that will be executed by the exec method if it exists

Let’s test, first of all in/App/Home/Controller. The class. The embedment hook in the PHP:





Then in/App/Home/plugins directory below set up and hook the same folder/App/Home/plugins/register below to build and hooks as file name register. PHP, write a simple class, name is also called the register, here are two methods, One called before and one called after:





Then register two hooks in the project’s common configuration:

/App/Conf/Hook.php





So when we access the Register method of the Index controller under Home, it will display:





So how exactly does this work? There is a Start method in the Cute core class that loads the routing startup framework and loads the /App/Conf/ hook.php file before loading the controller:





So in this file, our code looks like this:





The add method of Hook instantiates the register class in a private array that contains Hooks. This step is before the listen method. If the register class exists, execute the listen method. That’s how the hook functions.

PS: Much of the code and functions in this article are non-existent and only used to describe logical processes.

PPS: this paper uses a simple MVC framework written by myself in imitation of TP3.2.3. I only use it by myself. It is so simple that it makes everyone laugh.

PPPS: Beg collect, beg forward. Original is not easy to carry.