排除私人财产印刷

基本上我使用的是代码点火器,而代码点火器的基类是巨大的,当我print_r我的一些对象,他们的基类嵌入其中。 这让我很难得到我实际需要的信息(其余属性)。

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

我努力了

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

但当然,慈属性是私人的。

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

/**
 * 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');
}

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

// 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);

这是相当黑客,但工程 - 从对象中删除私有财产(这不会保留对象名称),当然还有一个缺点是您需要硬编码属性名称:

// 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["A_ci"]);

// Finally back to object
$b = (object) $b;

// Test it
dump($b);

您可以轻松使用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);
    }
}

方法是一样的。

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


我有一个简单的方法,适用于我:

<?php print_r(json_decode(json_encode($object))); ?>
链接地址: http://www.djcxy.com/p/66247.html

上一篇: exclude private property from print

下一篇: How to zoom in a Picturebox with scrollwheel in vb.net