Hooks, English for hooks. The application of the program is quite extensive, but what exactly is a hook? I introduce my current understanding of hooks and related experience.
If there is such a program flow:
Help
1234 |
function fun(){ funA(); funB(); } |
The normal execution order of the fun function is to execute the Funa, then execute the FUNB, then the fun function is finished. But, if we want to make some changes to the function. For example, fun is an analytic function, we hope that later can provide a rich analytic function, and which function to parse, we want to configure in the configuration file. The power of the hook can be played at this time.
We can do it in function fun () {}
After adding a hook point h, and then executing the H function, the hook function is configured so that I can parse it as needed.
For example:
Help
12345 |
$h =config_item ( " Parser_fun " //Get the appropriate configuration information from the configuration file function fun ( $data Code class= "php Spaces" >     global $h ;     return $h (); |
Of course, we have to have our own analytic function.
In addition to this unexpected, PHP can also provide its own class based on the string, and then invoke a method of the class, passing some parameters, which for the PHP program writing, as well as the later maintenance extension laid a fairly solid foundation. The
class is implemented in the following way:
$c =get_class_name ();//Get the name of the class
$m =get_method_name ();//Name of the method to obtain
$k = $c $m (); The execution of a method of the class
hooks in PHP application is quite extensive, the current author knows CodeIgniter and Drupal hooks in the application to make the following:
CodeIgniter application of hooks, The main is to apply all the hooks to a class hook, he extended the CI to the developer, CI provides a few hooks, in the codeigniter throughout the execution process, add some developers own functions, and in the hook class inside, Depending on the corresponding configuration in the external config.php and hooks.php, the corresponding class or individual function is obtained and then executed. Of course, these classes or methods are written by the developers themselves. This will allow developers to use more comfortable, feel better, after all, each developer should want to add more of their own things in a framework. For details, refer to the CodeIgniter official documentation, the section on hooks: CodeIgniter hook application.
and in Drupal the application of hooks is everywhere, he is by the hook to maintain a system, module,block,node the implementation of the various parts, are through the basic hook function, and then the function name after the corresponding change, Then call the function to get the entire hook structure, and even to the display layer theme implementation, all covering the theme theme API, more admirable one thing is, he incredibly can also use the visual angle, to achieve the corresponding theme hooks, In this way, the development of the theme is divided into two categories of developer and designer, the designer is more sensitive to the visualization, and the developer is more sensitive to the function. Both can make changes to Drupal from their own perspective. Not soon!