PHP 中的钩子是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5931324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:56:01  来源:igfitidea点击:

What is a hook in PHP?

phphook

提问by tasha-

I tried to search on Google but couldn't find any good tutorial or article.

我尝试在 Google 上搜索,但找不到任何好的教程或文章。

回答by BoltClock

You probably couldn't find anything because PHP doesn't have a concept of hooks in the first place.

您可能找不到任何东西,因为 PHP 一开始就没有钩子的概念。

Hooks are a kind of function which you can plug (or hook) to an existing system to extend its functionality. They aren't specific to the PHP language or to any system. They may also be called plugins, add-ons or extensions.

挂钩是一种功能,您可以将其插入(或挂钩)到现有系统以扩展其功能。它们不特定于 PHP 语言或任何系统。它们也可以称为插件、附加组件或扩展。

Now, while PHP doesn't have a concept of hooks, it does allow you to compile extensions together with the PHP core to gain added functionality for use in your scripts. There are plenty of PHP extensions bundled by default. This is an example of what I described above.

现在,虽然 PHP 没有钩子的概念,但它确实允许您将扩展与 PHP 核心一起编译,以获得在脚本中使用的附加功能。默认情况下捆绑了很多 PHP 扩展。这是我上面描述的一个例子。

回答by Atticus

Yeah, hooks aren't native PHP methods.. they're used to extend functionality from a framework's core.

是的,钩子不是原生的 PHP 方法..它们用于扩展框架核心的功能。

Codeigniter Hooks

Codeigniter 钩子

回答by alex

You can implement the observer pattern with some of the new SPL stuff, such as SplObserver().

您可以使用一些新的 SPL 内容来实现观察者模式,例如SplObserver().

It makes it easier to work with.

它使工作更容易。

回答by realmag777

You can emulate hooks in your own PHP project:

您可以在自己的 PHP 项目中模拟钩子:

1) Create and include next class:

1)创建并包含下一个类:

class Hooks {

    private static $actions = array(
        'ev_after_user_create' => array(),
        'ev_after_user_profile_update' => array()
    );

    public static function apply($hook, $args = array()) {
        if (!empty(self::$actions[$hook])) {
            foreach (self::$actions[$hook] as $f) {
                $f($args);
            }
        }
    }

    public static function add_action($hook, $function) {
        self::$actions[$hook][] = $function;
    }

}

Define there name of hooks you prefer.

定义您喜欢的钩子名称。

2) Now you can use hooks in you code, for example for do smth after new user created (example):

2)现在您可以在代码中使用钩子,例如在创建新用户后执行 smth(示例):

//here is going any code which creates new user  
//hooks
Hooks::apply('ev_after_user_create', array('user_id' => $new_user_id));

3) Define hooks actions in the next manner:

3)以下一种方式定义钩子动作:

Hooks::add_action('ev_after_user_create', function($args) {
    if (Router::$application === 'front') {
        require_model('users-data');
        $ud = new MUsersData(8);
        $ud->update_data($ud->create_page(), $args, 'id');
    }
});

Any hooks actions code should be defined BEFORE code where its action is needed!

任何钩子动作代码都应该在需要其动作的代码之前定义