php - 使用 OOP 在 wordpress 中添加 Action ?

标签 php

我正在学习 OOP,我想我已经掌握了它的窍门。我的问题是为什么 wordpress 样板插件的作者会这样写添加操作函数

add_action('admin_init', array(&$this, 'admin_init'));

根据我对 add_action Hook 的理解,要传递的参数是 $tag “$function_to_add Hook 的操作的名称”,以及您想要 Hook 的函数 $function_to_add

好吧,我理解 OOP 中的函数是方法,我明白为什么这可能会改变语法,但这只是一种模糊的表示,我想要一个明确的答案,为什么作者使用数组,然后使用 & $这个。我理解为什么有人会使用 $this->property 但不太确定 &$this

这只是您引用该方法的方式吗?如果是这样我仍然不理解数组,为什么它看起来不像 $this->admin_init

顺便说一句,结构看起来有点像这样

    class my_plugin_settings {

            public function __construct() {

                    // register actions
                    add_action('admin_init', array(&$this, 'admin_init'));

            } 

            public function admin_init() {

                    //Settings here
            }
    }

最佳答案

&这为 PHP 函数提供了上下文,这些函数将它连接到您的 admin_init 函数。查看 add_action 的声明或查看 http://www.php.net/manual/en/function.call-user-func-array.php .

这就是 PHP 如何知道使用该对象中的函数,而不是全局级函数

关于php - 使用 OOP 在 wordpress 中添加 Action ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18806674/

相关文章:

php - while(1) 与 for(;;) 中奇怪的内存使用

php - 如何使用 codeigniter 3 中的 dbforge 类在时间戳字段中设置更新属性

php - CakePHP:限制与模型关联的字段

php - 使用 js/ajax 将参数传递给 php 文档

php - 在 Symfony 的 CSS 文件中使用 PHP 变量

php - 如果我使用 CodeIgniter,清理 $_GET 以进行数据库搜索的最佳方法是什么?

php - 需要用户名和密码

php - 如何使用 PHP 以任意顺序(12 个字母,其中 6 个字母组成一个单词)进行字符搜索?

php - 如何在 PHP 中将文本文件加载到数据库?

php - 如何限制SQL Server中的查询执行时间?