标签:style blog http color io os 使用 ar strong
public static function objectToArray($d) {
if (is_object($d)) {
$d = get_object_vars($d);
}
if (is_array($d)) {
return array_map(array(__CLASS__, __FUNCTION__), $d);
} else {
return $d;
}
}
array_map(array(__CLASS__, __FUNCTION__), $d)解释:
我们可以在PHP手册中找到一段用户添加的说明:
If you need to 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函数回调内部方法要如何做呢?
先看一下代码(PS:由于文章长度限制,我只好去掉注释。。。):
<?php
/**
+-------------------------------------------------------------------------------------------
* @project SimpleObject
* @package SimpleObject
* @author Mc@Spring <Fuck.Spam@gmail.com>
* @version $ID: array.php Created on 2008-9-28 by Mc@Spring at 11:04:57 $
* @todo TODO
* @update Modified on 2008-9-28 by Mc@Spring at 11:04:57
* @link http://groups.google.com/group/mspring
* @copyright Copyright (C) 2007-2008 Mc@Spring. All rights reserved.
*
* Licensed under The Apache License
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License
+-------------------------------------------------------------------------------------------
*/
// (true === SO_SYS_ACCESS) || exit (‘System access denied!‘);
class Test{
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 class instantiate
//$return = array_map(array(self, ‘_entities‘), $arg);
// this will work under both static call and class instantiate
$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‘ => ‘<a href="http://www.baidu.com/?hi=1983&go=true">Simple Test</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));
// echo hightlight_file(__FILE__);
?>
这里有几点可以参考的:
1,在PHP类中通过array_map函数回调内部方法时,类名称可以使用__CLASS__常量。我们强烈推荐使用此常量,因为不论你类如何修改,这能保证最终结果都是正确的。
2,如果回调的方法是非静态类型,亦可通过$this伪变量指定。
3,在PHP类中的array_map函数总是不能识别self伪变量。
将对象转为数组方法:延伸array_map函数在PHP类中调用内部方法
标签:style blog http color io os 使用 ar strong
原文地址:http://www.cnblogs.com/leezhxing/p/4025809.html