2

我有一个将 php 变量记录到文件中的功能。有一个部分处理对象elseif(is_object($var))...,它适用于任何应用程序对象。但是如果变量是 StdClass 的对象,它就不起作用。我不明白其他对象和 StdClass 对象之间的区别在哪里。这是函数的代码:

function varLog( $var, $log = 'info', $depth = 2, $round = 0)
{
    $logFile = __DIR__ . '/../log/' . $log . '.log';
    file_put_contents( $logFile, '(' . gettype( $var ) . ') ', FILE_APPEND );

    if( in_array( gettype($var), ['integer', 'double', 'string'] ) )
    {
        file_put_contents( $logFile, $var . PHP_EOL, FILE_APPEND );
    }
    elseif( in_array( gettype($var), ['boolean', 'NULL'] ) )
    {
        $var = is_null( $var ) ? 'NULL' : ($var ? 'TRUE' : 'FALSE');
        file_put_contents( $logFile, $var . PHP_EOL, FILE_APPEND );
    }
    elseif ( is_array( $var ) )
    {
        file_put_contents( $logFile, 'length ' . count($var) . PHP_EOL, FILE_APPEND );

        foreach ( $var as $key => $val )
        {
            file_put_contents( $logFile, str_repeat('    ', $round + 1) . $key . ' => ', FILE_APPEND );
            if ( $round + 1 <= $depth )
            {
                varLog( $val, $log, $depth, $round + 1 );
            }
            else
            {
                file_put_contents( $logFile, '(' . gettype( $val ) . ')' . PHP_EOL, FILE_APPEND );
            }
        }
    }
    elseif ( is_object( $var ) )
    {
        file_put_contents( $logFile, get_class( $var ) . PHP_EOL, FILE_APPEND );
        $props = (new ReflectionClass( $var ))->getProperties();
        foreach ( $props as $prop )
        {
            $prop->setAccessible( true );
            $scoope = $prop->isPrivate() ? '(private)' : ($prop->isProtected() ? '(protected)' : '(public)');
            file_put_contents( $logFile, str_repeat('   ', $round + 1) . $scoope . ' ' . $prop->name . ' => ', FILE_APPEND );
            if ( $round + 1 <= $depth )
            {
                varLog( $prop->getValue( $var ), $log, $depth, $round + 1 );
            }
            else
            {
                file_put_contents( $logFile, '(' . gettype( $prop->getValue( $var ) ) . ')' . PHP_EOL, FILE_APPEND );
            }
        }
    }
}

好像线

$props = (new ReflectionClass( $var ))->getProperties();

不返回任何道具。

4

1 回答 1

2

PHP 有ReflectionClassReflectionObject。您可以ReflectionObject使用StdClass.

$props = (new ReflectionObject(($var))->getProperties();

否则,您可以使用get_object_vars

$props = get_object_vars($var);

两者的区别在于ReflectionClass只会返回类的原始属性。

假设您有以下类:

class Test {
    public $foo;
}

然后创建它的一个实例并分配一个新属性:

$instance = new Test;
$instance->bar = 'foo';

然后,如果您使用以下命令检索类的属性ReflectionClass

(new ReflectionClass($instance))->getProperties();

它不返回bar属性:

[
    ReflectionProperty {#3059
        +name: "foo",
        +class: "Test",
        modifiers: "public",
    },
]

因此使用ReflectionClasswith时的空数组StdClass。而ReflectionObject将返回两者:

(new ReflectionObject($instance))->getProperties();

输出是:

[
    ReflectionProperty {#3071
        +name: "foo",
        +class: "Test",
        modifiers: "public",
    },
    ReflectionProperty {#3073
        +name: "bar",
        +class: "Test",
        modifiers: "public",
    },
]

来源:https ://gist.github.com/wjaspers/9353164

于 2019-04-16T18:48:44.193 回答