遍历对象php的所有属性
问题描述:
如何遍历对象的所有属性?现在,我必须编写新的代码行来打印对象的每个属性
How can I loop through all the properties of object?. Right now I have to write a new code line to print each property of object
echo $obj->name;
echo $obj->age;
我可以使用foreach循环或任何循环遍历对象的所有属性吗?
Can I loop through all the properties of an object using foreach loop or any loop?
类似这样的东西
foreach ($obj as $property => $value)
答
如果这仅用于调试输出,则还可以使用以下内容查看所有类型和值.
If this is just for debugging output, you can use the following to see all the types and values as well.
var_dump($obj);
如果您想对输出进行更多控制,可以使用以下方法:
If you want more control over the output you can use this:
foreach ($obj as $key => $value) {
echo "$key => $value\n";
}