【问题标题】:How to make PHP StdClass reflection如何使 PHP StdClass 反射
【发布时间】:2019-04-16 18:24:30
【问题描述】:

我有一个函数可以将 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();

不返回任何道具。

【问题讨论】:

  • 如果需要,您可以使用get_object_vars($var);,而不是使用ReflectionClass 作为StdClass
  • 我想知道为什么这段代码不适用于 StdClass。 get_object_vars() 只返回公共属性。

标签: php reflection stdclass


【解决方案1】:

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",
    },
]

因此,当您将ReflectionClassStdClass 一起使用时,会出现空数组。而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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-16
    • 2013-10-23
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多