获取对象所包含的类方法及相关数据

作者:Jeen 发布于:2013-4-21 19:54 Sunday 分类:Web开发

在Magento开发模板的时候,我们经常需要从产品或其他对象中取得一些可用方法

当然基本的 print_r(get_class_method(get_class($temp_object)));  已经基本够用了,但是输出的格式 等等还是看起来有点让人头疼,这边稍微格式化了一下。

多的不说了直接上代码:

function objInfo($temp_obj){ //show object class methods data information
$temp_class = get_class($temp_obj);
if(!$temp_class) {
echo “\$$temp_obj  is not an object ~!<br>”;
print_r($temp_obj);
echo “<hr>”;
return 0;
}
$details = ”;
$temp_methods = get_class_methods($temp_class);
$details .= (‘The Object of Class: ‘.$temp_class.’ –totaly has ‘.count($temp_methods).’ Methods<hr>’);
$temp_vars = get_class_vars($temp_class);
foreach($temp_methods as $temp_method){
$details .= (“\$object->”.$temp_method.”()<br>”);
}
$details .= ‘<hr>’;
if(in_array(‘debug’,$temp_methods)) {
$details .= ‘<h2>debug() method returns</h2><hr>’;
$temp_data = $temp_obj->debug();
foreach($temp_data as $_var => $_value){
if(!is_array($_value))  $details .= ($_var.’ === ‘.$_value.”<hr>”);
else {
$details .= $_var .” === array (“;
foreach($_value as $t_var => $t_value)
$details .=  (‘ ['.$t_var.' = '.$t_value.'] ‘);
$details .= ‘)’;
}
}
}
if(in_array(‘getData’,$temp_methods)) {
$details .= ‘<h2>getData() method returns</h2><hr>’;
$temp_data = $temp_obj->getData();
foreach($temp_data as $_var => $_value){
$details .= ($_var.’ === ‘.$_value.”<hr>”);
}
}
print_r($details);
return 1;
}//objInfo end;
objInfo($_product); //输出$_product 的相关信息

开始是考虑用反射API来取得一些更详细的信息,发现有点太画蛇添足了。。。

就简单点做个备忘吧…

标签: magento php

发表评论:

©2010-2024 Jeen All Rights Reserved.Powered by emlog 京ICP备15058100号-1