


Detailed explanation of ThinkPHP behavioral extensions and plug-ins
本文主要介绍了ThinkPHP中的行为扩展和插件原理分析、ThinkPHP中的行为、自定义行为、自定义插件等,给大家提供参考,希望对大家有所帮助。
原理分析
将标签与类之间的对应关系(如'app_init'=>array('Common\Behavior\InitHook')
),通过Hook类中import或add方法,加载到Hook类中静态变量$tags中。当执行Hook中静态方法listen
或者exec
方法的时候(listen
方法中调用了exec
),实例化标签对应的类,调用相应的方法(如果是插件,调用传递的方法,如果是行为,调用run方法)。
Hook中exec方法定义如下:
1 2 3 4 5 6 7 |
|
ThinkPHP中的行为 加载标签与类之间的对应关系
// 加载模式行为定义
1 2 3 4 |
|
模式行为对应关系
在ThinkPHP/Mode/common.php中tags标签中定义
1 2 3 4 5 6 7 |
|
应用行为对应关系
在Application/Common/Conf/tags.php中定义
调用相应的行为
如Hook::listen('app_begin')
相当于调用Behavior\ReadHtmlCache
中的run方法。找到该类,可以查看其对应的run方法如下
自定义行为 1.在Application/Common/Conf/tags.php中添加对应关系
1 |
|
将对应关系添加到tags.php,程序会自动将其加载到Hook的$tags变量中,当然也可通过使用Hook中的add方法手动加载。
2.定义Common\Behavior\DqsBehavior类
1 2 3 4 5 6 7 |
|
其中Behavior 类为ThinkPHP自带的抽象类
3.调用行为
效果如下图
自定义插件 1.定义插件文件
通过查看Hook文件中插件类实例化方式($class = "Addons\\{$name}\\{$name}Addon";
),可以得知,如果想定义一个名为Dqs的插件,应该在Addons/Dqs目录下定义DqsAddon.class.php文件。程序如下:
1 2 3 4 5 |
|
2.注册插件
所谓的注册插件,就是将插件的标签与类之间的对应关系,添加到Hook类中静态变量$tags中。对应行为来说,标识符仅仅是一个标识符,可以定义任意的标识符,但是对应插件来说,标识符不能随意定义,因为插件的标识符代表的是插件的方法名。所以上面的插件注册代码如下
1 |
|
3.调用插件
1 2 |
|
效果如下
原理分析
将标签与类之间的对应关系(如'app_init'=>array('Common\Behavior\InitHook')
),通过Hook类中import或add方法,加载到Hook类中静态变量$tags中。当执行Hook中静态方法listen
或者exec
方法的时候(listen
方法中调用了exec
),实例化标签对应的类,调用相应的方法(如果是插件,调用传递的方法,如果是行为,调用run方法)。
Hook中exec方法定义如下:
1 2 3 4 5 6 7 |
|
ThinkPHP中的行为 加载标签与类之间的对应关系
// 加载模式行为定义
1 2 3 4 |
|
模式行为对应关系
在ThinkPHP/Mode/common.php中tags标签中定义
1 2 3 4 5 6 7 |
|
应用行为对应关系
在Application/Common/Conf/tags.php中定义
调用相应的行为
如Hook::listen('app_begin')
相当于调用Behavior\ReadHtmlCache
中的run方法。找到该类,可以查看其对应的run方法如下
自定义行为 1.在Application/Common/Conf/tags.php中添加对应关系
1 |
|
将对应关系添加到tags.php,程序会自动将其加载到Hook的$tags变量中,当然也可通过使用Hook中的add方法手动加载。
2.定义Common\Behavior\DqsBehavior类
1 2 3 4 5 6 7 |
|
其中Behavior 类为ThinkPHP自带的抽象类
3.调用行为
效果如下图
自定义插件 1.定义插件文件
通过查看Hook文件中插件类实例化方式($class = "Addons\\{$name}\\{$name}Addon";
),可以得知,如果想定义一个名为Dqs的插件,应该在Addons/Dqs目录下定义DqsAddon.class.php文件。程序如下:
1 2 3 4 5 |
|
2.注册插件
所谓的注册插件,就是将插件的标签与类之间的对应关系,添加到Hook类中静态变量$tags中。对应行为来说,标识符仅仅是一个标识符,可以定义任意的标识符,但是对应插件来说,标识符不能随意定义,因为插件的标识符代表的是插件的方法名。所以上面的插件注册代码如下
1 |
|
3.调用插件
1 2 |
|
效果如下
相关推荐:
thinkPHP5使用laypage分页插件实现列表分页功能_php实例
The above is the detailed content of Detailed explanation of ThinkPHP behavioral extensions and plug-ins. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
