【问题标题】:php function cannot return objectphp函数不能返回对象
【发布时间】: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
                                                    (
                                                    )

                                            )

                                    )

                            )

                    )

            )

    )

)

【问题讨论】:

标签: php arrays object return


【解决方案1】:
            $this->getChildObjectById($childObject, $newObjType, $newObjId);

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

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

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

编辑:

除了缺少返回值之外,您的函数可能无法正常工作还有其他原因:

  • getChildObjects() 可能不会返回您期望的结果
  • 子对象可能不是作为参数传递的类的实例

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

看起来您的搜索功能正试图用一块石头杀死两只鸟。有点像 id 是子结构的一种索引,因此传递 0(您的默认值)将允许访问给定类型的第一条记录。这使得它使用起来很复杂而且相当麻烦。

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

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

【讨论】:

  • 所以我查看了 Barmar 的另一个问题并查看了您的回复。它们看起来确实很相似,但是当我尝试修复时,这段代码现在变为: return $this->getChildObjectById($childObject, $newObjType, $newObjId);我仍然没有取回我的对象​​数据。
  • id 与我存储在 MySQL 数据库中的匹配,它们不是任意的。我将尝试将其分解为两个单独的函数,一个用于检查实例对象的类,另一个用于检查 id。
  • 你说得对,我想多了。问题解决了,最终重建了大约 50% 的对象类并将检查分解为单个函数。递归数组有效,我确实成功地在递归调用前使用了“return”。感谢您的帮助 kuroi! :)
猜你喜欢
  • 2011-12-28
  • 1970-01-01
  • 2012-03-27
  • 2011-02-27
  • 1970-01-01
  • 1970-01-01
  • 2022-12-04
  • 2020-04-23
  • 1970-01-01
相关资源
最近更新 更多