gpt4 book ai didi

php - &$this 在 Wordpress add_action 回调中

转载 作者:搜寻专家 更新时间:2023-10-31 21:10:09 24 4
gpt4 key购买 nike

我刚刚接触 Wordpress,我正在尝试使用 OOP 技术编写一个非常简单的插件。我一直在关注本教程:http://www.yaconiello.com/blog/how-to-write-wordpress-plugin/ .到目前为止,我觉得我理解了大部分正在发生的事情,但我对这样的陈述感到有些困惑:

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

已阅读 Wordpress's add_action() 上的文档和 PHP callables ,我认为第二个参数是类实例的方法。但是我不明白为什么$this 必须通过引用传递

在 PHP 文档中发现了这个关于可调用对象的注释,我怀疑这可能与它有关,但我仍然很难弄明白它们之间的区别是什么:

Note: In PHP 4, it was necessary to use a reference to create a callback that points to the actual object, and not a copy of it. For more details, see References Explained.

如果我有 PHP 5,我只使用 array($this,'init') 安全吗?

可能相关:add_action in wordpress using OOP?

最佳答案

是的 - 你可以安全地使用 array($this, 'init');

这实际上是 PHP 中的“可调用”。它将使用 call_user_func()call_user_func_array() 调用(在 add_action 方法内部)。

PHP官方对这种可调用类型的描述:

A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1.

您可以阅读更多关于可调用项的信息 here .

关于php - &$this 在 Wordpress add_action 回调中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21766137/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com