php函数无法返回对象
PHPV5.4中面向对象的新编码器,遇到了一个奇怪的障碍。我有一个递归遍历对象层次结构的函数,它根据对象的类类型和唯一ID匹配对象,然后返回它:php函数无法返回对象,php,arrays,object,return,Php,Arrays,Object,Return,PHPV5.4中面向对象的新编码器,遇到了一个奇怪的障碍。我有一个递归遍历对象层次结构的函数,它根据对象的类类型和唯一ID匹配对象,然后返回它: public function getChildObjectById($searchObj, $newObjType, $newObjId = 0) { /* iterate through each child object in the array */ foreach($searchObj->getChildObjects
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
}
}
}
现在,当我在getChildObjectById函数中对$childObject进行打印时,我得到了我要查找的对象及其所有属性/值,因此$childObject被正确定义,搜索似乎正在工作。但是,下一行我尝试将其返回调用函数时,不知何故丢失了该对象,并且对$parentItemGroup执行var_转储(期望得到我的对象)只会导致NULL
这里是否有足够的人指出我缺少的内容,或者您需要查看更多代码?我是不是离基地太远了
提前感谢您能提供的任何帮助,并且要温柔。。。我还在学习!:)
下面是我正在研究的对象数组的一个示例:
$objFound = $childObject->getId() == $newObjId; // no need for ? TRUE : FALSE
您忘记返回递归调用的结果
顺便说一句,布尔值最好用作布尔值:)
编辑:
除了缺少返回值之外,还有其他原因导致函数无法工作:
- getChildObjects()可能不会返回您期望的结果
- 子对象可能不是作为参数传递的类的实例
无论如何,如果你想调试你的函数,我建议你放弃类类型检查,看看会发生什么。可能重复的,所以我看了Barmar的另一个问题,看了你的回答。它们看起来很相似,但当我尝试修复时,这段代码现在变成了:return$this->getChildObjectById($childObject,$newObjType,$newObjId);我仍然没有取回我的对象数据。ID与我存储在MySQL数据库中的内容匹配,它们不是任意的。我将尝试将其分为两个单独的函数,一个用于检查实例化对象的类,另一个用于检查id。你是对的,我想得太多了。问题解决了,最终重建了大约50%的对象类,并将检查分解为单个函数。递归数组可以工作,我确实成功地在递归调用前面使用了“return”。谢谢你的帮助,kuroi!:)
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
(
)
)
)
)
)
)
)
)
$this->getChildObjectById($childObject, $newObjType, $newObjId);
$objFound = $childObject->getId() == $newObjId; // no need for ? TRUE : FALSE