Principle and Simple Application of Hook of hook in php Example of demo
- 2021-12-19 06:10:00
- OfStack
In this paper, the principle and simple application of hook (hook) in php are described with examples. Share it for your reference, as follows:
Let's review the original development process first;
Product Wang has created a pile of demand;
When the user registers successfully, he needs to send short messages, send emails and so on;
Then the smart, witty and brave Cheng Xuyuan jumped on it;
Convert these requirements into code and throw them between successful user registration and jumping to the home page;
Nothing can stop it; Creative apes;
<?php
class Test{
public function index(){
// User registration succeeded
/*
Here is 1 Heap the code for sending short messages
*/
/*
Here is 1 Code for Heap Sending Mail
*/
/*
Here is 1 Code for other functions of the heap
*/
// Go to the homepage of the website
}
}
$test=new Test();
$test->index();
If each function is performed by a different ape;
The first thing you face is that the code will be messy; It will be troublesome to cooperate;
Then encapsulate it as a function; 1 aspect will be standardized and neat; In addition, it is convenient to call repeatedly;
Nothing can stop it; Creative apes;
<?php
class Test{
public function index(){
// User registration succeeded
// Send a text message
sendSms($phone);
// Send Mail
sendSms($email);
// Other operations ...
// Go to the homepage of the website
}
}
/**
* Send SMS notification
* @param integer $phone Mobile phone number
*/
function sendSMS($phone){
// Here is the code for sending short messages
}
/**
* Send mail notification
* @param string $email Email address
*/
function sendEmail($email){
// Here is the code to send the message
}
At this time, the operation said;
If you can click a button in the background, you can set whether to send an email or send a short message; That must have been excellent;
Nothing can stop it; Creative apes;
<?php
class Test{
public function index(){
// User registration succeeded
if (' If Send SMS is set up ') {
// Send a text message
sendSms($phone);
}
if (' If you set up Send Mail ') {
// Send Mail
sendSms($email);
}
// Other operations ...
// Go to the homepage of the website
}
}
/**
* Send SMS notification
* @param integer $phone Mobile phone number
*/
function sendSMS($phone){
// Here is the code for sending short messages
}
/**
* Send mail notification
* @param string $email Email address
*/
function sendEmail($email){
// Here is the code to send the message
}
It is no problem to do this in a closed enterprise environment;
Ran goose; We also have an open and selfless ape leader who wants to open up the program for the benefit of other apes;
It is hoped that more apes will participate in this project; Co-development function;
If everyone changes this program; Throw your own code between the success of user registration and jumping to the home page;
This is obviously unreliable; Think about the confusion of 1 collapse and confusion;
Can you put your own code in a directory?
Then the system automatically loads these codes according to the configuration items between the user registration success and jumping to the home page?
Well, first define the following directory
plugin//Plug-in Directory
→ plugin1//Plug-in 1
Configuration Items for config. php//Plug-in 1
The program processing content of-index. php//Plug-in 1
→ plugin2
→ → config. php
→ → index. php
-plugin3
→ → config. php
→ → index. php
----...
index. php//Business Logic
Code for the business logic:
<?php
class Test{
public function index(){
// User registration succeeded
// Get all plug-ins
$pluginList=scandir('./plugin/');
// Loop plug-in // Exclude . ..
foreach ($pluginList as $k => $v) {
if ($v=='.' || $v=='..') {
unset($pluginList[$k]);
}
}
echo " Simple background management <hr>";
// Plug-in management
foreach ($pluginList as $k => $v) {
// Getting Configuration Items
$config=include './plugin/'.$v.'/config.php';
$word=$config['status']==1 ? ' Click Close ' : ' Click Open ';
echo $config['title'].'<a href="./index.php?change='.$v.'" rel="external nofollow" >'.$word.'</a><br />';
}
echo '<hr>';
// Output plug-in content
foreach ($pluginList as $k => $v) {
// Getting Configuration Items
$config=include './plugin/'.$v.'/config.php';
if ($config['status']==1) {
include './plugin/'.$v.'/index.php';
// Run plug-ins
Hook::run($v);
}
}
// Go to the homepage of the website
}
}
// Plug-in class
class Hook{
// Register to add plug-ins
public static function add($name,$func){
$GLOBALS['hookList'][$name][]=$func;
}
// Execute plug-in
public static function run($name,$params=null){
foreach ($GLOBALS['hookList'][$name] as $k => $v) {
call_user_func($v,$params);
}
}
}
// Change plug-in status
if (isset($_GET['change'])) {
// Getting to Configuration Items
$config=include './plugin/plugin'.substr($_GET['change'],-1).'/config.php';
// If it is turned on Then close it If it is closed Open
$config['status']=$config['status']==1 ? 0: 1;
// Write the changed configuration item to a file
$str="<?php \\r\\n return ".var_export($config,true).';';
file_put_contents('./plugin/'.$_GET['change'].'/config.php', $str);
header('Location:./');
}
$test=new Test();
$test->index();
Plug-in configuration item code:
<?php
return array (
'status' => 1, // Definition status 1 Indicates opening 0 Indicates closing
'title' => ' Send a text message ', // The name of the plug-in
);
Content of the plug-in:
That's right; This is the idea of plug-ins;
Of course, this is only a super simple example;
The complete plug-in mechanism should include plug-in type, database, audit and so on;
If you have used wordpress or domestic discuz;;
You will find that a good program is not just how good it is;
And what matters is how well the design expands; How convenient it is for everyone to expand its functions;
If you want to study plug-ins in depth; It is recommended to read the source code of wordpress and discuz.
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 article is helpful to everyone's PHP programming.