...因为如果父对象为空,则会foreach
显示错误。for()
将是我首选的循环逻辑,但我不知道如何访问子对象属性。
工作代码:
foreach ($main_object as $object) {
$object->value = $object->value * 3.14;
}
但是当$main_object
为空或空时,会foreach
转储出错误。
我正在寻找更像这样的东西:
(伪)代码:
for ($p = 0; $p < counter($main_object); $p++) {
//$p being the index'd id of the child object. That is where I'm having issues, how do I refer to child X within a parent object.
$main_object->$p->value = $main_object->$p->value * 3.14;
}
现在,如果'$main_object' 为空/null,则不会引发错误,逻辑会继续运行。
$x->properties
当对象是父对象内的一组子对象之一时,如何访问子对象?