PHP的array_map函数使用类内部方法作为回调函数的实现方式

来源:互联网 发布:测试手机电池的软件 编辑:程序博客网 时间:2024/06/06 19:05
在PHP编程中,我们经常会遇到处理数组的单元数据问题,比如对数组中每个单元应用自定义函数。

一种方法是通过循环遍历整个数组,对每个单元调用自定义函数,然后用返回值替换原数组相应单元的值。这也是最常见和简单的方法,在此就不举例了。

一种方法是通过PHP提供的array_map函数回调自定义函数,这也是被推荐的方法。

array_map --将回调函数作用到给定数组的单元上

说明:

array array_map ( callback callback,array arr1 [, array ...] )

array_map() 返回一个数组,该数组包含了 arr1中的所有单元经过 callback 作用过之后的单元。callback 接受的参数数目应该和传递给 array_map()函数的数组数目一致。

摘录一段PHP手册的列子简要说明如下:

<?php
function cube($n){
    return
 $n * $n * $n;
}

$a = array(1, 2, 3, 4, 5);
$b= array_map("cube", $a);
print_r($b);
?>


那么,如果你是在一个PHP类中通过array_map函数回调内部方法又该如何做呢?

同样,我们可以在PHP手册中找到一段用户添加的说明:

If you needto call a static method from array_map, this will NOT work:
(如果你想在array_map函数中回调一个静态方法,那么下面的做法是错误的)

<?php
$a = array(1, 2, 3, 4, 5);
$b =
 array_map("myclass::myMethoed",$a);
print_r($b);
?>


Instead, you need to do this:
(你应该做如下调用)

<?php
$a = array(1, 2, 3, 4, 5);
$b =
 array_map(array("myclass","myMethoed"),$a);
print_r($b);
?>


感谢作者的分享,因为PHP手册中对array_map函数的参数说明确实太过简单,以至于连基本的对象方法引用都没提及。

现在进入我们讨论的主题:如果在PHP类中通过array_map函数回调内部方法要如何做呢?

先看一下代码:

<?php

// (true === SO_SYS_ACCESS) || exit ('System accessdenied!');

classTest
{
    public function
 __construct
(){}
    
    public function
 common_filter($arg
){
        return
 $this->entities($arg
);
    }
    
    public function
 public_static_filter($arg
){
        return
 self::_entities($arg
);
    }
    
    public function
 private_static_filter($arg
){
        return
 self::__entities($arg
);
    }
    
    public function
 entities($arg
){
        
$return= null
;
        if(
is_array($arg
)){
            
$return= array_map(array($this, 'entities'), $arg
);
        }else{
            
$return= is_numeric($arg) ? $arg : htmlspecialchars($arg, ENT_QUOTES
);
        }
        return
 $return
;
    }
    
    public static function
 _entities($arg
){
        
$return= null
;
        if(
is_array($arg
)){
            
//this will neithor work under static call nor classinstantiate
            //$return = array_map(array(self, '_entities'), $arg);
            
            // this will work under both static call and classinstantiate
            
$return= array_map(array(__CLASS__, '_entities'), $arg
);
        }else{
            
$return= is_numeric($arg) ? $arg : htmlspecialchars($arg, ENT_QUOTES
);
        }
        return
 $return
;
    }
    
    private static function
 __entities($arg
){
        
$return= null
;
        if(
is_array($arg
)){
            
$return= array_map(array(__CLASS__, '__entities'), $arg
);
        }else{
            
$return= is_numeric($arg) ? $arg : htmlspecialchars($arg, ENT_QUOTES
);
        }
        return
 $return
;
    }
}

$args
=array(
    
'name'=>'Mc\'Spring'
,
    
'age'=>25
,
    
'email'=>'Fuck.Spam@gmail.com'
,
    
'address'=>
'<ahref="http://www.baidu.com/?hi=1983&go=true">SimpleTest</a>'
);

print_r(Test::_entities($args));

echo
 '<br/>'
;

$obj= new Test
;

print_r($obj->entities($args
));

echo
 '<br/>'
;

print_r($obj->common_filter($args
));

echo
 '<br/>'
;

print_r($obj->public_static_filter($args
));

echo
 '<br/>'
;

print_r($obj->private_static_filter($args));
 

// echohightlight_file(__FILE__);
?>


这里有几点可以参考的:

1,在PHP类中通过array_map函数回调内部方法时,类名称可以使用__CLASS__常量。我们强烈推荐使用此常量,因为不论你类如何修改,这能保证最终结果都是正确的。

2,如果回调的方法是非静态类型,亦可通过$this伪变量指定。

3,在PHP类中的array_map函数总是不能识别self伪变量。
0 0