【发布时间】:2020-05-11 17:58:53
【问题描述】:
我的班级(posts.php):
class Posts {
private function getPosts() {
$get_post = (new MYSQL) -> getAllPosts(); // here get all posts from my db
$GLOBALS["all_posts"] = $get_posts;
function all_posts() {
// When I use the return, the page enter on one infinite bucle.. If I use echo this doesnt happen.
return $GLOBALS["all_posts"];
}
}
}
我希望在我的 content.php 中调用 all_posts() 函数来获取数组并像这样打印:
<div class="posts">
<?php foreach(all_posts() AS $post) : ?>
<h1><?php echo $post["title"]</h1>
<p><?php echo $post["content]; ?></p>
<?php endforeach; ?>
</div>
我希望函数 all_posts() 可以加载到我的 content.php 中;在我的 index.php 中,在包含 header.php、content.php 和 footer.php 之前,我加载了 Post->getPosts()。谢谢你。
【问题讨论】:
标签: php arrays function class globals