稍微先说些体外话吧
很多人都会说yii框架很屌,thinkphp也很多人用,国内的phpcms,织梦简直逆天
wordpress框架太烂了
看看
“apply_filters”和"actions"就知道了,功能弱爆了
那么我倒想问问大家
“apply_filters”和"add_filter"有什么区别
这时候,很多人就会马上取查api,(这我多次提到,最low b的人才会去查api)
apply_filters比add_filter多了1个s,没看到么?
我不是在说笑
apply_filters 多了一个s的意思就是filter可能有n个,大部分情况不会是一个
如果apply_filters只有一个,那么此模组方法的名字就会是apply_filter而不是apply_filters
人家wordpress的光是命名已经是标准得不能再标准
明白我的题外话了么,1个屋子有多屌,并不是你以为多屌就能多屌,并不是意识形态决定的,人家如果门面都刷得很漂亮,那么屋子里面也一般比较屌
说远了,我们再来分析下apply_filters和apply_actions是怎么实现的
方法
public function apply_filters( $value, $args ) {
在类wp-includes/class-wp-hook.php
(ctm,又真的没有什么秘密,要不要我们做老师的活啊,人家就明显告诉你这是一个钩子)
private $iterations = array();
public function apply_filters( $value, $args ) {
if ( ! $this->callbacks ) {
return $value;
}
$nesting_level = $this->nesting_level++;
$this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
$num_args = count( $args );//下面省略
}
代码再次告诉你。。。。。。array是php里面的神器,和javascript的prototype一样,所有api文档都不会告诉你一个事实,array就是神器,要好好学
(还在研究hashtable,map,有什么区别的同学,还在想用堆栈还是链表的同学,事实就是什么才是有用的,看出来了么?)
2.方法apply_filters里面有一些对数组统计,排优先顺序的操作。。。。。也是php里面很经典的array操作
$nesting_level = $this->nesting_level++;
$this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
$num_args = count( $args );
(事实是,并没有apply_actions的方法,没必要copy & paste)
/**
* Executes the callback functions hooked on a specific action hook.
*
* @since 4.7.0
* @access public
*
* @param mixed $args Arguments to pass to the hook callbacks.
*/
public function do_action( $args ) {
$this->doing_action = true;
$this->apply_filters( '', $args );
// If there are recursive calls to the current action, we haven't finished it until we get to the last one.
if ( ! $this->nesting_level ) {
$this->doing_action = false;
}
}
我也不懂英文,所以姑且这么理解,1个action = filters的集合
所以do_action就是filters的封装而已,这不就是传说中的“重用”么。。。不一定要面向对象的。。。而且是很直观的。。。懂吗
4.wordpress框架是什么时候,在哪里引入hook的?