0

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

                                            )

                                    )

                            )

                    )

            )

    )

)
lordvig
  • 155
  • 1
  • 5
  • possible duplicate of [Recursive function return null value](http://stackoverflow.com/questions/21520556/recursive-function-return-null-value) – Barmar Feb 03 '14 at 08:06

1 Answers1

0
            $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.

kuroi neko
  • 8,479
  • 1
  • 19
  • 43
  • so I looked at the other question from Barmar and looked at your response. They do seem to be similar, but when I try the fix, so this code now becomes: return $this->getChildObjectById($childObject, $newObjType, $newObjId); I'm still not getting my object data back. – lordvig Feb 03 '14 at 16:31
  • the ids match what I'm storing in the MySQL database, they are not arbitrary. I'll try breaking that into two separate functions, one to check class of instanced object and one to check the id. – lordvig Feb 03 '14 at 16:59
  • You were right, I was overthinking it. Problem solved, ended up rebuilding about 50% of my object classes and broke checks into single functions. Recursive array works, and I did use the "return" in front of the recursive call successfully. Thanks for your help kuroi! :) – lordvig Feb 04 '14 at 02:14