2016-11-06 39 views
2

当使用PHP库RTM(https://github.com/bartosz-maciaszek/php-rtm),我得到一个特定的任务列表这样的回应:如何访问对象内有美元符号的保护数组键(来自RTM-php的响应)?

[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'};失败这里.....所以,我如何访问这些数据?

+0

尝试'$ note_obj - > {'get $ t'}();' – Dekel

+0

@Dekel Nope。那也行不通。 'PHP致命错误:Uncaught BadMethodCallException:方法获取$ t没有在https://github.com/bartosz-maciaszek/php-rtm/blob/master/src/Rtm/DataContainer.php'...........looking [DataContainer.php](https://github.com/bartosz-maciaszek/php-rtm/blob/master/src/Rtm/DataContainer.php)我找到了一种将对象转换为数组的方法。可能是这样的路要走。我会尝试在这里发表评论。 –

+0

尝试var_dump(get_class_methods($ note_obj));'查看是否有任何可用于获取所有值的特定方法。 – Dekel

回答

0

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

这些方法也可以通过暴露(如在评论所指出@Dekel):

​​给出

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"; 

完成!