如何遍历protected object转化为数组

<?php

class test{
protected $testOne = 1;
protected $testTwo = 'string';
}

$test = new test;

var_dump($test);

/**
*ReflectionClass::getDefaultProperties
*获取类的默认属性(包括了继承的属性)
*这个函数不区分静态和非静态属性,也不考虑可见性修饰符
*/
$refClass = new \ReflectionClass($test);

$dataArr = [];

foreach ($refClass->getDefaultProperties() as $key=>$val) {
  $dataArr[$key] = $val;
}

var_dump($dataArr);

posted @ 2017-12-01 10:34  神龙诀  阅读(355)  评论(0编辑  收藏  举报