Public Static functionObjecttoarray ($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 ($d) explains:
We can find a user-added description in the PHP manual:
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", Span style= "color: #800080;" > $a print_r ( $b ? >
< Span style= "color: #ff00ff;" >
Instead, you need to does this:
(you should make the following call)
<? PHP $a Array (1, 2, 3, 4, 5); $b Array_map (array$a); Print_r ($b);
Thanks to the author for sharing, because the parameter descriptions of the Array_map function in the PHP manual are really too simple to mention even the basic object method references.
Now let's go to the topic we're talking about: what do you do if you call the internal method through the Array_map function in a PHP class?
First look at the code (PS: Due to the length of the article, I had to remove the comments ...) ):
<?php
/**
+-------------------------------------------------------------------------------------------
* @project Simpleobject
* @package Simpleobject
* @author [email protected] <[email protected]>
* @version $ID: array.php Created on 2008-9-28 by [email protected] at 11:04:57 $
* @todo Todo
* @update Modified on 2008-9-28 by [email protected] at 11:04:57
* @link http://groups.google.com/group/mspring
* @copyright Copyright (C) 2007-2008 [email protected] 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! ');
ClassTest{
Public function__construct(){}
Public functionCommon_filter($arg){
Return$this-Entities($arg);
}
Public functionPublic_static_filter($arg){
ReturnSelf::_entities($arg);
}
Public functionPrivate_static_filter($arg){
ReturnSelf::__entities($arg);
}
Public functionEntities($arg){
$return=Null;
IfIs_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;
IfIs_array($arg)){
This would neithor work under static call nor class instantiate
$return = Array_map (Array (self, ' _entities '), $arg);
This would 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;
IfIs_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 '=' [Email protected] ',
' 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__);
?>
Here are a few things you can refer to:
1, the class name can be used when the internal method is recalled through the Array_map function in the PHP class__class__Constant.We strongly recommend that you use this constant, because no matter how you modify the class, this will ensure that the final result is correct.
2, if the callback method is non-static type, you can also pass$thisA pseudo-variable is specified.
3, the Array_map function in the PHP class is not always recognized SelfPseudo variables.
To convert an object to an array method: Extend the Array_map function to invoke an internal method in a PHP class