Renegade Rob Renegade Rob - 3 months ago 10
PHP Question

PHP Trying to get property of non-object error in function

Need some help please. I am getting a 'Trying to get property of non-object' error in a function call that looks at an array.

The array I am calling is

$var = array(
"variableA" => "abc123",
"variableB" => "123456789"
);


The function I am using is

public function getJson($var)
{
$resource = sprintf("/info/%s/%s/json", $var->variableA, $var->variableB);
return $this->_restCall('GET', $resource);
}


I cant understand why the array values are not being passed through?

Could someone please help?

Answer

$var is an array not an object. So you need to use array syntax, not object syntax:

public function getJson($var)
{
    $resource = sprintf("/info/%s/%s/json", $var['variableA'], $var['variableB']);
    return $this->_restCall('GET', $resource);
 }
Comments