2010-08-18 112 views
58

在JSON编码的谷歌的API返回的对象,如本如何访问在php中作为变量命名的对象属性?

[updated] => stdClass Object 
(
[$t] => 2010-08-18T19:17:42.026Z 
) 

任何人都知道我怎么能访问$t价值?

$object->$t显然返回

Notice: Undefined variable: t in /usr/local/...

Fatal error: Cannot access empty property in /....

回答

136

这应该工作:

echo $object->{'$t'}; 

做不到这一点:

$property_name = '$t'; 
echo $object->{$property_name}; 
+0

+1与我的答案相同。 – 2010-08-18 19:32:54

+0

谢谢,这工作! – 2010-08-18 19:33:00

+0

+1我以为我知道所有关于PHP的知识。感谢您的教育。 – 2010-08-18 19:42:03

13

你试过:

$t = '$t'; // Single quotes are important. 
$object->$t; 
+0

工作了,谢谢! – 2010-08-18 19:33:40

+0

记录行为:http://php.net/manual/en/language.variables.variable.php有一些重要的笔记,请阅读它;) – 2017-03-16 16:44:10

0

我使用php7,以下工作适合我:

class User { 
    public $name = 'john'; 
} 
$u = new User(); 

$attr = 'name'; 
print $u->$attr;