Reputation: 1137
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
Reputation: 4820
There appears to be a few problems here with your combined function. In a nutshell...
$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
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