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


How do I access protected array key having a dollar sign inside a object (response from RTM-php)?

当使用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'};在这里失效.....那么我如何访问这些数据呢?

我发现类的所有方法都是由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";

完成了!