如何访问对象内部有美元符号的受保护数组键(来自RTM-php的响应)?

时间:2016-11-06 15:33:28

标签: php arrays object protected

当使用php库进行RTM(https://github.com/bartosz-maciaszek/php-rtm)时,我收到特定任务列表的响应,如下所示:

[notes] => Rtm\DataContainer Object

(
    [attributes:Rtm\DataContainer:private] => Array
        (
            [note] => Rtm\DataContainer Object
               (
                   [attributes:Rtm\DataContainer:private] => Array
                       (
                           [id] => 56254802
                           [created] => 2016-11-06T10:46:43Z
                           [modified] => 2016-11-06T10:49:26Z
                           [title] => null
                           [$t] => https://stackoverflow.com/questions/910912/extract-urls-from-text-in-php1
                       )

               )

        )

)

我可以获得id, created, modified的价值,但 $t无效。

$note_obj = $obj->getNotes()->getNote();
$note_id = $note_obj->getId();
echo "$note_id\n";  //works fine

$note_content = $note_obj->get{'$t'}(); //doesn't work
print_r($note_content); 

显然$note_obj->get{'$t'};在这里失败了.....所以如何访问这些数据呢?

1 个答案:

答案 0 :(得分:0)

我发现所有类的方法都由DataContainer.php处理,它有toArray之类的方法将对象转换为数组。

这些方法也可以通过(正如@Dekel在评论中指出)公开:

var_dump(get_class_methods($note_obj));给出了

array(10) {
[0]=>
string(11) "__construct"
[1]=>
string(11) "getIterator"
[2]=>
string(5) "count"
[3]=>
string(6) "__call"
[4]=>
string(3) "get"
[5]=>
string(3) "set"
[6]=>
string(3) "has"
[7]=>
string(6) "remove"
[8]=>
string(7) "toArray"
[9]=>
string(6) "toJson"
}

因此代码是:

$note_obj = $obj->getNotes()->getNote();
$rtm_item_note_content = $note_obj->toArray();
$rtm_item_note_content = $rtm_item_note_content['$t'];
echo "note content: $rtm_item_note_content\n";

完成!