Welcome Guest, Not a member yet? Register   Sign In
A function calling itself.
#1

[eluser]John Morton[/eluser]
I'm relatively new to CI so I'm unsure if I'm headed down the right path on this one, but here I am. (I'm from the EE side.)

I'm polling an API that is returning a nested object. I would like to loop through this object and turn it into an array. The problem I'm running into is when the function doing that looping tries to call itself when it needs to.

Here's the function that lives in disqus_model.php:

Code:
function objectToArray( $object )
        {
            if( !is_object( $object ) && !is_array( $object ) )
            {
                return $object;
            }
            if( is_object( $object ) )
            {
                $object = get_object_vars( $object );
            }
            return array_map( $this->objectToArray , $object );
        }

It seems to be doing it's job on the first pass through. The first level of my returned object is reported as an array by a print_r, but when the function tries to call itself on the last line, I'm getting this error reported:

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: My_model::$objectToArray

Filename: models/disqus_model.php

Line Number: 54

Since the error says $objectToArray is an unfound property, I think it's looking for a variable called that. I'm intended for it to see the actual function and use that again.

I think I'm using the wrong syntax to referencing my function, but I've tried a variety of things without success.

Can you offer any advice?


Messages In This Thread
A function calling itself. - by El Forum - 08-19-2010, 03:23 PM
A function calling itself. - by El Forum - 08-19-2010, 04:40 PM
A function calling itself. - by El Forum - 08-19-2010, 05:01 PM
A function calling itself. - by El Forum - 08-19-2010, 06:26 PM
A function calling itself. - by El Forum - 08-19-2010, 07:57 PM



Theme © iAndrew 2016 - Forum software by © MyBB