【问题标题】:exclude private property from print_r or object?从 print_r 或对象中排除私有财产?
【发布时间】:2012-11-09 06:40:22
【问题描述】:

基本上我使用的是 Code Igniter,而且 Code Igniter 基类很大,当我 print_r 我的一些对象时,它们中嵌入了基类。这使得获取我真正想要的信息(其余属性)变得很痛苦。

所以,我想知道是否有一种方法可以隐藏或删除基类对象?

我试过了

clone $object;
unset($object->ci);
print_r($object);

当然 ci 属性是私有的。

我用于倾倒的实际功能是:

/**
 * Outputs the given variables with formatting and location. Huge props
 * out to Phil Sturgeon for this one (http://philsturgeon.co.uk/blog/2010/09/power-dump-php-applications).
 * To use, pass in any number of variables as arguments.
 * Optional pass in "true" as final argument to kill script after dump
 *
 * @return void
 */
function dump() {
    list($callee) = debug_backtrace();
    $arguments = func_get_args();
    $total_arguments = count($arguments);
    if (end($arguments) === true)
        $total_arguments--;

    echo '<fieldset style="background: #fefefe !important; border:2px red solid; padding:5px">';
    echo '<legend style="background:lightgrey; padding:5px;">' . $callee['file'] . ' @ line: ' . $callee['line'] . '</legend><pre>';

    $i = 0;
    foreach ($arguments as $argument) {
        //if the last argument is true we don't want to display it.
        if ($i == ($total_arguments) && $argument === true)
            break;

        echo '<br/><strong>Debug #' . (++$i) . ' of ' . $total_arguments . '</strong>: ';

        if ((is_array($argument) || is_object($argument)) && count($argument)) {
            print_r($argument);
        } else {
            var_dump($argument);
        }
    }

    echo '</pre>' . PHP_EOL;
    echo '</fieldset>' . PHP_EOL;

    //if the very last argument is "true" then die
    if (end($arguments) === true)
        die('Killing Script');
}

【问题讨论】:

    标签: php debugging properties


    【解决方案1】:

    这应该适用于只返回未知类的公共变量:

    // Get the class of the object
    $class_of_object= get_class($object);
    
    // Get only public attributes (Note: if called from within class will return also private and protected)
    $clone = get_class_vars($class_of_object);
    
    // Try it
    dump($clone);
    

    这很 hacky,但可以 - 从对象中删除私有属性(这不会​​保留对象名称),当然另一个缺点是您需要对属性名称进行硬编码:

    // First cast clone to array
    $b = (array) clone($a);
    
    // Then unset value (There will be null bytes around A and to use them you need to run it in double quotes
    // Replace A for * to remove protected properties
    unset($b["\0A\0_ci"]);
    
    // Finally back to object
    $b = (object) $b;
    
    // Test it
    dump($b);
    

    【讨论】:

    • 但我想要其他私有财产,我只是不想要那个财产
    • 但是get_object_vars() 需要一个对象作为参数。 $class_of_object 是一个字符串。
    • @TheFox 是的,抱歉一定是错字,应该是get_class_vars()
    【解决方案2】:

    您可以使用 PHP Reflextion API 轻松完成

    $myClassName = 'myChildClass';
    $reflection = new ReflectionClass($myClassName);
    
    // get properties, only public in this case
    $properties = $reflection->getProperties(ReflectionMethod::IS_PUBLIC);
    //Print all properties (including parent class)
    print_r($properties);
    
    //Print properties of desired class only
    foreach ($properties as $property) {
        if ($property->class == $myClassName) {
            print_r($property);
        }
    }
    

    方法也一样。

    如果您真的需要它 - 您可以创建特殊功能来为您完成这项工作,并在您需要此类分析时调用它。但我认为在大多数情况下,好的 IDE,比如我最喜欢的 PHPStorm,可能会为您完成这项工作,并且当您调用类实例时 - 仅在建议列表中向您显示公共方法。

    【讨论】:

    • 我还使用 PHPStorm care 来添加有关 PHPStorm 如何为您执行此操作的信息?
    • 好吧,当你输入 $obj->[ctrl+Enter] 时,它只是不显示私有方法或属性。
    【解决方案3】:

    我有一个适合我的简单方法:

    <?php print_r(json_decode(json_encode($object))); ?>
    

    【讨论】:

      猜你喜欢
      • 2013-11-27
      • 1970-01-01
      • 2012-06-22
      • 2015-08-23
      • 2016-11-17
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多