Magento格式化输出调试对象的类和方法

by Web全栈工程师 on 2011 年 12 月 23 日

我们在开发Magento模板的时候,需要获取对象的一些方法,但是使用print_r(get_class_method(get_class($object))) 输出的格式太凌乱,需要格式化下输出结果,我们可以定义个输出格式函数objInfo(),如下:

<?php
 function objInfo($temp_obj) {
    $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;
?>

Comments on this entry are closed.

{ 1 trackback }

Previous post:

Next post: