【发布时间】:2014-02-26 15:23:45
【问题描述】:
PHP v5.4 中面向 OOP 的新编码器并遇到了奇怪的问题。我有一个函数,它递归地遍历对象的层次结构,通过对象的类类型和唯一 ID 匹配对象,然后返回它:
public function getChildObjectById($searchObj, $newObjType, $newObjId = 0) {
/* iterate through each child object in the array */
foreach($searchObj->getChildObjects() as $childObject) {
/* check if object is an instance of specified type */
if (is_object($childObject) && $childObject instanceof $newObjType) {
/* checks if the object id was matched */
$objFound = ($childObject->getId() == $newObjId);
if ($objFound) {
echo "*** found the object ***<br>";
echo "Found child object type: " . $newObjType . "<br>";
echo "id: " . $childObject->getId() . "<br>";
echo "<pre>";
print_r($childObject); // <-- object information populated here
echo "</pre>";
return ($childObject); // <-- lose it here
}
/**
* checks if the object has children. If so, recursively call function to continue
* searching the multidimensional array
*/
if (count($childObject->getChildObjects() > 0)) {
echo "child object has " . count($childObject->getChildObjects()) . " direct children:<br>";
echo "returning childObject:<br>";
echo "<pre>";
print_r($childObject); // <-- child object information populated here
echo "</pre>";
echo "retuning objtype: " . $newObjType . "<br>"; // <-- ItemGroup
echo "returning objId: " . $newObjId . "<br>"; // <-- 11
return $this->getChildObjectById($childObject, $newObjType, $newObjId);
} // as per suggestion added "return" to front of this call, still getting NULL and error message Fatal error: Call to a member function addChildObject() on a non-object on line 187
}
}
}
从这一行调用函数:
$parentItemGroup = $newCollection->getChildObjectById($newCollection, 'ItemGroup', $parentId);
现在,当我在 getChildObjectById 函数中对 $childObject 执行 print_r 时,我得到了我正在寻找的对象及其所有属性/值,因此 $childObject 被正确定义并且搜索似乎正在工作。但是,我尝试将其返回给调用函数的下一行以某种方式丢失了对象并执行 $parentItemGroup 的 var_dump 期望我的对象只会导致 NULL。
这里是否有足够的内容让某人指出我遗漏的内容或者您需要查看更多代码?我在这里离基地很远吗?
提前感谢您提供的任何帮助,并且要温柔...我仍在学习! :)
这是我正在研究的对象数组的一个示例:
Collection Object
(
[id:DataObject:private] => 1
[properties:DataObject:private] => Array
(
[title] => Collection One
)
[childObjects:DataObject:private] => Array
(
[0] => Item Object
(
[id:DataObject:private] => 6
[properties:DataObject:private] => Array
(
[title] => Item Six
)
[childObjects:DataObject:private] => Array
(
)
)
[1] => Item Object
(
[id:DataObject:private] => 11
[properties:DataObject:private] => Array
(
[title] => Item Eleven
)
[childObjects:DataObject:private] => Array
(
)
)
[2] => Item Object
(
[id:DataObject:private] => 10
[properties:DataObject:private] => Array
(
[title] => Item Ten
)
[childObjects:DataObject:private] => Array
(
)
)
[3] => Item Object
(
[id:DataObject:private] => 14
[properties:DataObject:private] => Array
(
[title] => Item Fourteen
)
[childObjects:DataObject:private] => Array
(
)
)
[4] => ItemGroup Object
(
[id:DataObject:private] => 1
[properties:DataObject:private] => Array
(
[item_group_title] => 1
[item_group_depth] => 0
[item_group_parent_id] =>
)
[childObjects:DataObject:private] => Array
(
[0] => Item Object
(
[id:DataObject:private] => 7
[properties:DataObject:private] => Array
(
[title] => Item Seven
)
[childObjects:DataObject:private] => Array
(
)
)
[1] => Item Object
(
[id:DataObject:private] => 1
[properties:DataObject:private] => Array
(
[title] => Item One
)
[childObjects:DataObject:private] => Array
(
)
)
[2] => ItemGroup Object
(
[id:DataObject:private] => 5
[properties:DataObject:private] => Array
(
[item_group_title] => 1.4
[item_group_depth] => 1
[item_group_parent_id] => 1
)
[childObjects:DataObject:private] => Array
(
[0] => Item Object
(
[id:DataObject:private] => 2
[properties:DataObject:private] => Array
(
[title] => Item Two
)
[childObjects:DataObject:private] => Array
(
)
)
)
)
[3] => ItemGroup Object
(
[id:DataObject:private] => 2
[properties:DataObject:private] => Array
(
[item_group_title] => 1.1
[item_group_depth] => 1
[item_group_parent_id] => 1
)
[childObjects:DataObject:private] => Array
(
[0] => Item Object
(
[id:DataObject:private] => 8
[properties:DataObject:private] => Array
(
[title] => Item Eight
)
[childObjects:DataObject:private] => Array
(
)
)
)
)
)
)
)
)
【问题讨论】: