不能将类型的对象用作数组 VS 数组到字符串的转换


Cannot use object of type as array VS Array to string conversion

所以我正在做这个项目,我正处于分页系统的点。每当调用页面时,它都会按以下方式进行:

索引页根据$_GET变量搜索页面:

if (isset($_GET['page']))
{
    $tpl->parsePage($_GET['page']);
}
    else
{
    $tpl->parsePage('index');
}

然后页面的名称通过我的tpl类中的parsePage函数:

public function parsePage($pagename)
    {
        $pageid = $this->getPageIdByName($pagename);
        $page = $this->shorts(new Page($pageid));
        return $page;
    } 

它使用函数获取页面的 ID,然后创建一个新页面。在页面类中,页面的内容是从我的数据库中获取的:

public function __construct($pageid)
    {
        $pageid = $pageid['id'];
        $query = DB::$conn->prepare('SELECT * FROM pages WHERE id = :id LIMIT 1');
        $query->bindParam(':id', $pageid, 'PDO::PARAM_INT);
        $query->execute();
        $pageInfo = $query->fetch('PDO::FETCH_ASSOC);
        $this->pageid = $pageInfo['id'];
        $this->title = $pageInfo['title'];
        $this->authors = $pageInfo['authors'];
        $this->contents = $pageInfo['contents'];
        $this->publish_time = $pageInfo['publish_time'];
        $this->edit_time = $pageInfo['edit_time'];
        $this->edits = $pageInfo['edits'];
        $this->editor = $pageInfo['editor'];
        $this->hidden = $pageInfo['hidden'];
        $this->parent = $pageInfo['parent'];
        $this->category = $pageInfo['category'];
    }

然后,$this->contents数据通过一个函数,该函数将所有参数替换为其分配的值:

private function shorts($content)
{
    if(isset($_SESSION['user']))
    {
        $this->parameter = array_merge($this->parameter, $this->user);
    }
    $this->parameter = array_merge($this->parameter, $this->language, $this->system);
    return $this->output = str_replace(array_keys($this->parameter), array_values($this->parameter), $content->contents); // Line 34
}

这就是它出错的地方。当我使用这种方式(带$content->contents)时,输出如下:

Notice: Array to string conversion in C:'xampp2'htdocs'application'classes'class.tpl.php on line 34

我想到的第一件事:$content['contents'],但随后输出如下:

Fatal error: Cannot use object of type C_Red'Template'Page as array in C:'xampp2'htdocs'application'classes'class.tpl.php on line 34 

我的知识到此为止...

我已经在短裤功能中标记了行34。我希望这个问题足够清楚,可以理解,但我真的不知道如何描述它,因为这是一个非常模糊的错误。

谢谢。

$content是对象。$content->内容是一个数组。

所以$content['content']不起作用,因为$content是一个不可迭代的对象。我不知道 Page 类的结构,但根据你所说的,也许这会起作用:

$content->contents['contents']