dourong8495 2014-02-03 08:03
浏览 34
已采纳

php函数无法返回对象

newish coder for OOP in PHP v5.4 and running into an odd snag. I have a function that recursively iterates through a hierarchy of objects, matches an object by its class type and its unique ID, and then returns it:

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
        }
    }
}

The function is called from this line:

                $parentItemGroup = $newCollection->getChildObjectById($newCollection, 'ItemGroup', $parentId);

Now when I do a print_r on the $childObject within the getChildObjectById function I have the object I'm looking for and all its properties/values so the $childObject is defined properly and the search appears to be working. However, the next line where I try and return that back to the calling function somehow loses the object and doing a var_dump of $parentItemGroup expecting my object just results in NULL.

Is there enough here for someone to point out what I'm missing or do you need to see more code? Am I way off base here?

Thanks in advance for any assistance you can provide, and be gentle... I'm still learning! :)

Here's an example of the object array I'm working towards:

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条回答 默认 最新

  • douhe3313 2014-02-03 08:10
    关注
                $this->getChildObjectById($childObject, $newObjType, $newObjId);
    

    You forgot to return the result of your recursive call

    btw. boolean values are better used as booleans :)

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

    EDIT:

    Beside the missing return, there are other reasons your function might not work:

    • getChildObjects() might not return what you expect
    • the child objects might not be instances of the class passed as parameter

    My gut feeling is that you are overdoing this. Why don't you assign a unique id to each object (regardless of its class)?

    It looks like your search function is trying to kill two birds with one stone. A bit like if the id was a kind of index into your sub-structures, so that passing 0 (your default value) would allow to access the 1st record of a given type. It makes it complex and rather cumbersome to use.

    I would rather have a function explicitely retrieve objects by ids and another perform a directory search.

    Anyway, if you want to debug your function I suggest you drop the class type check and see what happens.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测