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