(array)
class Pre1
{
private $name = 'aaa' ;
private $age = 100;
}
class Pre2 extends Pre1
{
private $name= 'bbb' ;
public $age= 111;
}
var_dump ((array) new Pre2 ());
得到结果
array(4) {
["Pre2name"]=> string(3) "bbb"
["age"]=> int(111)
["Pre1name"]=> string(3) "aaa"
["Pre1age"]=> int(100)
}
private 的属性会变成类名+属性名转换成数组的键名,其值为属性的值
(object)
$arr = ['aa'=>'aaa', 'bb'=>'bbbb', 'cc'=>'ccc'];
$obj = (object) $arr;
echo $obj->bb;
注意点:数组最好为关联数组,转换成对象后就是属性与属性值的关系
原文链接:https://blog.csdn.net/qq_28137309/article/details/81415367