PHP尝试在函数中获取非对象错误的属性
问题描述:
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?
需要一些帮助。 我在查看数组的函数调用中得到“试图获取非对象属性”错误。 p>
我调用的数组是 p>
$ var = array(
“variableA”=>“abc123”,
“variableB”=>“123456789”
);
code> pre>
我正在使用的函数是 p>
公共函数getJson($ var)
{
$ resource = sprintf(“/ info /%s /% s / json“,$ var-> variableA,$ var-> variableB);
返回$ this-> _restCall('GET',$ resource);
}
code> pre >
我无法理解为什么没有传递数组值? p>
有人可以帮忙吗? p>
div>
答
$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);
}