如何进行 PHP StdClass 反射

问题描述 投票:0回答: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();

不返回任何道具。

php reflection stdclass
2个回答
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",
    },
]

因此,当您将

ReflectionClass
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


0
投票

FWIW PHP < 7.0 has/had a bug

$obj = (object) array(
    // stdClass with numeric prop names/keys
    'foo',
    'bar',
);
$ref = new ReflectionObject($obj);
var_dump($ref->getProperties(); // empty array

$obj = (object) array(
    'a' => 'foo',
    'b' => 'bar',
);
$ref = new ReflectionObject($obj);
var_dump($ref->getProperties(); // non-empty
© www.soinside.com 2019 - 2024. All rights reserved.