Jump to content

Recursive function in a class


girish.kc

Recommended Posts

Hi,

 

I am using a separate class for all the functions. And using using the following function "objectToArray" to convert the JSON decoded object to Array.

 

Class file:

     


               class xyz
              {
                    . 
                    .
                    .
                    .
                    .

                    function objectToArray($d) {
	         if (is_object($d)) {
	 	     $d = get_object_vars($d);
        	         }
	         if (is_array($d)) {
		     return array_map(__FUNCTION__, $d);
	         }
	         else {
		     return $d;
	         }
          }
                    .
                    .
                    .

          }
      

 

 

Main page:

          $obj = new xyz();

          $arr = $obj->objectToArray($json_obj);

 

Because the function "objectToArray" is recursive, array_map(__FUNCTION__, $d) is not able to applies the callback. And throws the warning "array_map() expects parameter 1 to be a valid callback".

 

Need help!

 

Girish

Link to comment
https://forums.phpfreaks.com/topic/217557-recursive-function-in-a-class/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.