-2

I am working on a small function which will convert objects to strings. It is easy if object has values as arrays. But I want this function to work even my object or array has some values as another object or array. I make it very coarse so experts please help this to make it tidy for everyone.

My function:

function makeString($array)
{
    $outval = "";  
    foreach($array as $key=>$value) {
        if (is_object($value)) {
            $arr = array();
            $arr = get_object_vars($value);

            foreach($arr as $key1=>$value1){
                if(is_array($value1)) { 
                    $outval .= "\t\t$key1\n"; 
                    $outval .= makeString($value1);
                }  
                else {
                    $outval .= "\t$key1: $value1\n";
                } 

            }
        }
        if(is_array($value)) { 
            $outval .= "\t$key\n"; 
            $outval .= makeString($value);
        }  
        else {
            $outval .= "$key: $value\n";
        }  
    } 
    return $outval;  
}  

This part is recurring inside my function:

...
    if (is_object($value)) {
        $arr = array();
        $arr = get_object_vars($value);

        foreach($arr as $key1=>$value1){
            if(is_array($value1)) { 
                $outval .= "\t\t$key1\n"; 
                $outval .= makeString($value1);
            }  
            else {
                $outval .= "\t$key1: $value1\n";
            } 

        }
    }
...

What if $value1 here is also an object? What should I do to make this function work even if my object has many other objects or arrays inside main object.

aiternal
  • 1,080
  • 3
  • 16
  • 33
  • why would you do this instead of just use json_encode? You're also re-implementing a built in function: __toString – AD7six Jan 13 '13 at 21:04
  • I just tried json_encode but it returns something that cannot be written on file. Thank you but just forget about it buddy. People thinks this is not a common question. So.. – aiternal Jan 14 '13 at 19:49

1 Answers1

1

Your function suffers from a flaw which comes from your one assumption: That the function will be passed an array. However, as you're finding out, this is not guaranteed.

So, a better way to design such a function is to not assume anything about the input, and restructure your function like this:

function makeString( $value) {
    if( is_object( $value)) { }
    else if( is_array( $value)) { }
    else if( is_string( $value) || is_numeric( $value)) { }
    else { } // Resource, etc.
}
nickb
  • 59,313
  • 13
  • 108
  • 143
  • Ok, but I needed this function while I am working on Xen APIs. It has some variables as objects. I am digging into it and the variable also has another object or array inside as value. So I need something that would convert whole object to human readable text. – aiternal Jan 13 '13 at 18:47
  • That's great, but you have to write it. Nobody is going to write the entire function for you. I've shown you how you need to restructure your function so it will work, now you have to put in the recursion and finish the function. – nickb Jan 13 '13 at 18:48
  • Well, thank you! I just saw there is no sample on internet for this need and if we could put a well-designed sample here lot of people would use this. That coarse function works for me already. – aiternal Jan 13 '13 at 18:54