这条线是什么意思?

时间:2014-11-29 04:22:37

标签: php oop pdo

我是PHP的初学者。我想知道this文件中的第47行,意思是:

$posts[$key]['comments'] = $this->comments->commentNumber($post['id']);

我正在浏览SitePoint中名为“Jump Start PHP”的书,但我并不完全理解这一行。另一个导致缺乏理解的问题是本书没有提供的缺失模式。

感谢您的时间。

2 个答案:

答案 0 :(得分:1)

$this->comments类注释的对象,其中包含名为 commentNumber 的方法/函数

$posts[$key]['comments'] = $this->comments->commentNumber($post['id']);

在此语句中,将在类注释的对象上调用commentNumber函数,并将返回值分配给索引$ key处的变量$ post。

答案 1 :(得分:0)

$this->comments

在类Posts的类构造函数中的第20行实例化。基本上,"评论"的新实例class被实例化并设置为$ this-> comments。

致电时:

$posts[$key]['comments'] = $this->comments->commentNumber($post['id']);

它正在调用第90行 - 类注释的commentNumber方法。该方法接受帖子的ID并返回该帖子的评论数量。

此行及其相关循环的目的是遍历所有帖子并获取每个帖子的评论数量。一旦循环完成,数组$ posts将会 包含有关帖子的所有信息(每个数组索引一个帖子),包括评论数量。