php函数无法返回对象

时间:2014-02-03 08:03:31

标签: php arrays object return

在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
                                                    (
                                                    )

                                            )

                                    )

                            )

                    )

            )

    )

)

1 个答案:

答案 0 :(得分:0)

            $this->getChildObjectById($childObject, $newObjType, $newObjId);

您忘记返回递归调用的结果

顺便说一句。布尔值最好用作布尔值:)

$objFound = $childObject->getId() == $newObjId; // no need for ? TRUE : FALSE

编辑:

除了缺失的回报之外,还有其他原因导致您的功能失效:

  • getChildObjects()可能无法返回您期望的内容
  • 子对象可能不是作为参数传递的类的实例

我的直觉是你过度了。为什么不为每个对象分配一个唯一的id(不管它的类是什么)?

看起来你的搜索功能试图一石二鸟。有点像id是你的子结构的一种索引,所以传递0(你的默认值)将允许访问给定类型的第一条记录。这使得它使用起来很复杂且相当麻烦。

我宁愿让一个函数通过id显式检索对象,另一个执行目录搜索。

无论如何,如果你想调试你的功能,我建议你放弃班级类型检查,看看会发生什么。