解析错误:类stdClass的对象无法转换为字符串

时间:2014-02-26 05:44:06

标签: php codeigniter parsing templates

我正在使用twitter api检索收藏推文,然后使用模板解析它们:

$favs_list = $this->connection->get('favorites/list');
$data_to_parse['fav_list'] = $fav_list;
$html = $this->parser->parse('templates/tweet_list', $data_to_parse, TRUE);
$output['html'] = $html;
$this->load->view('read', $output);

模板tweet_list只是:

<h3>Favs:</h3>
{fav_list}
<h5>{text}</h5>
{/fav_list}

但我一直收到这两个错误,并重复多次:

A PHP Error was encountered    
Severity: 4096     
Message: Object of class stdClass could not be converted to string     
Filename: libraries/Parser.php
Line Number: 143

A PHP Error was encountered   
Severity: Notice     
Message: Object of class stdClass to string conversion     
Filename: libraries/Parser.php    
Line Number: 143

奇怪的是,在这些错误下,我的html以正确的格式显示。 有什么原因引起了这个想法吗?

2 个答案:

答案 0 :(得分:2)

我不知道最佳方法,但我通常使用

json_decode(json_encode ($favs_list), true);

答案 1 :(得分:0)

您正在尝试打印favs_list,但内容是一个对象,并且很难尝试转换该对象。

只需将对象转换为数组,然后使用

$array = (array) $object;

在你的情况下

$favs_list = $this->connection->get('favorites/list');
$data_to_parse['fav_list'] = (array) $fav_list;