PHP MongoDB\Model\BSONDocument 不做反思
PHP MongoDB\Model\BSONDocument does not make a reflection
有人可以解释一下为什么我的 PHP 代码无法反映 MongoDB\Model\BSONDocument 吗?有一行 elseif(is_object($var))
正确捕获了一个对象但没有任何属性。其他 PHP 个对象已正确记录。也是私有和受保护的值。为什么 BSONDocument 不能?
function varLog( $var, $log = 'info', $deph = 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 <= $deph )
{
varLog( $val, $log, $deph, $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 <= $deph )
{
varLog( $prop->getValue( $var ), $log, $deph, $round + 1 );
}
else
{
file_put_contents( $logFile, '(' . gettype( $prop->getValue( $var ) ) . ')' . PHP_EOL, FILE_APPEND );
}
}
}
}
你应该看看这里的 class:https://github.com/mongodb/mongo-php-library/blob/master/src/Model/BSONDocument.php
它没有任何属性。
有人可以解释一下为什么我的 PHP 代码无法反映 MongoDB\Model\BSONDocument 吗?有一行 elseif(is_object($var))
正确捕获了一个对象但没有任何属性。其他 PHP 个对象已正确记录。也是私有和受保护的值。为什么 BSONDocument 不能?
function varLog( $var, $log = 'info', $deph = 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 <= $deph )
{
varLog( $val, $log, $deph, $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 <= $deph )
{
varLog( $prop->getValue( $var ), $log, $deph, $round + 1 );
}
else
{
file_put_contents( $logFile, '(' . gettype( $prop->getValue( $var ) ) . ')' . PHP_EOL, FILE_APPEND );
}
}
}
}
你应该看看这里的 class:https://github.com/mongodb/mongo-php-library/blob/master/src/Model/BSONDocument.php
它没有任何属性。