How can I access an object property named as a variable in php?

前端 未结 5 1677
陌清茗
陌清茗 2020-12-04 16:04

A Google APIs encoded in JSON returned an object such as this

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

Anyone

5条回答
  •  無奈伤痛
    2020-12-04 16:30

    I'm using php7 and the following works fine for me:

    class User {
        public $name = 'john';
    }
    $u = new User();
    
    $attr = 'name';
    print $u->$attr;
    

提交回复
热议问题