Home > Backend Development > PHP Tutorial > wordpress 中的hook机制,在原生php脚本中怎么简洁的表达实现

wordpress 中的hook机制,在原生php脚本中怎么简洁的表达实现

WBOY
Release: 2016-06-13 12:01:42
Original
831 people have browsed it

wordpress 中的hook机制,在原生php脚本中如何简洁的表达实现?
wordpress 中的hook(钩子)机制,也就是add_action和do_action这样的组合钩子功能,在原生php脚本中如何来实现?例如有

add_action('main','function1');<br />add_action('main','function2');<br />do_action('main');
Copy after login

这样的功能,在原生php中怎么来简洁的表达实现?
谢谢
------解决方案--------------------
本帖最后由 xuzuning 于 2014-04-03 20:03:07 编辑

$hook = array();<br /><br />function main() {<br />  global $hook;<br />  foreach($hook as $fn) {<br />    if(function_exists($fn)) $fn();<br />  }<br />}
Copy after login

用于没有规则的约束,参数的传递是个问题(太自由了)

一般宜用类实现,比较容易附加约束

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template