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

[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:

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:

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?

Try changing your return statement to:

return array_map( array($this, __FUNCTION__), $object );

[eluser]John Morton[/eluser]

You rock. That worked perfectly.

I'm not familiar with:

array($this, __FUNCTION__)

I did a search on "array($this, __FUNCTION__)" and see the top hit on the php.net on the array_map page. It's listed deep in the comments there. Can you tell me how you knew? If not, it's no problem. I'm glad the problem is solved. Thanks!


You're welcome. Glad it worked.

__FUNCTION__ is just an easy way to make sure that if you change your function name, it will still be called recursively if you forget to change the code that does the recursive call. That's it. Smile

The array notion is a bit harder for me to explain (because I've just never thought it through.) Basically, it means call the current object method statically. Doesn't make a whole lot of sense to me, but this is a standard way of calling methods from within the same instantiated object when using callback functions (like array_map(), array_walk(), etc ) that I've just learned to use through experience. Perhaps it preserves the scope of the current object somehow? If anyone can give a decent explanation, please do share. Smile


Actually the PHP manual states:
Quote:A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1.

I guess this has nothing to do with static objects but instantiated ones and it's just how you're supposed to call them. So forget my static call crap. Smile

[eluser]John Morton[/eluser]
Very good! It actually makes sense. Much appreciated.

Theme © iAndrew 2016 - Forum software by © MyBB