Jason
Jason

Reputation: 1137

Issue with combining two PHP functions

I'm trying to combine two functions into a single function but I'm having more difficulty than I expected. I have included the two separate functions and the version where I tried to combine them. The variable requestData holds the object that I'm iterating. However, in my combined version I keep getting a fatal error of Call to undefined method on the line that has $this->requestData($value).

I feel like I'm missing something simple so apologies for the newbie question. Thank you in advance for any assistance!

FUNCTION #1:

public function iterateRequestKeys($array)
    {
        foreach ($array as $key => $value)
            {
                if (is_object($value))
                    {
                        $this->iterateRequestKeys($value);
                    }
                else
                    {
                        $updated_value = '1234';
                        $array->$key = $updated_value;
                    }   
            }
    }

FUNCTION #2:

public function requestFindReplace($resourceID, $requestData)
    {
        $this->iterateRequestKeys($requestData);    
    }

COMBINED:

public function requestFindReplace($resourceID, $requestData)
    {
        foreach ($requestData as $key => $value)
            {
                if (is_object($value))
                    {
                        $this->requestData($value);
                    }
                else
                    {
                        $updated_value = '123';
                        $array->$key = $updated_value;
                    }   
            }   
    }

Upvotes: 0

Views: 37

Answers (2)

e_i_pi
e_i_pi

Reputation: 4820

There appears to be a few problems here with your combined function. In a nutshell...

  1. You're not calling the recursive function and you're not passing the parameters through (line 7)
  2. You're not passing by reference either (which I believe you may well need to do with an array, not entirely sure I'm a but rusty on PHP). (line 1)
  3. You're attempting to update $array which does not exist (line 12)

Try this code instead:

public function requestFindReplace($resourceID, &$requestData)
    {
        foreach ($requestData as $key => $value)
            {
                if (is_object($value))
                    {
                        $this->requestFindReplace($resourceID, $value);
                    }
                else
                    {
                        $updated_value = '123';
                        $requestData->$key = $updated_value;
                    }   
            }   
    }

Upvotes: 0

Captain Insaneo
Captain Insaneo

Reputation: 470

It looks like it's you're recursively calling requestFindReplace(), so you'd want to change that to line to $this->requestFindReplace($resourceID, $value);

public function requestFindReplace($resourceID, $requestData)
{
    foreach ($requestData as $key => $value)
    {
        if (is_object($value))
        {
            $this->requestFindReplace($resourceID, $value);
        }
        else
        {
            $updated_value = '123';
            $array->$key = $updated_value;
        }   
    }   
}

Upvotes: 1

Related Questions