本文介绍了当不在对象上下文中时使用$ this的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个函数来显示博客。所以我做了一个显示博客功能,但它一直给使用$这当不在对象上下文错误

I'm creating a function to show blog's. So I made a show blog function but it keeps giving "Using $this when not in object context" error

Class Blog{

    public function getLatestBlogsBig($cat = null){
        $sqlString = "SELECT blog_id FROM jab_blog";
        if($cat != null)
            $sqlString .= " WHERE blog_cat = " . $cat;

        $sqlString .= " ORDER BY blog_id DESC LIMIT 5";
        $blog = mysql_query($sqlString);

        while($id = mysql_result($blog,"blog_id")){
            $this->showBlog($id); //Error is on this line
        }

    }

    function showBlog($id,$small = false){
        $sqlString = "SELECT blog_id FROM jab_blog WHERE blog_id=" . $id . ";";
        $blog = mysql_query($sqlString);

        if($small = true){
            echo "<ul>";
            while($blogItem = mysql_fetch_array($blog)){
                echo '<a href="' . $_SESSION['JAB_LINK'] . "blog/" . $blogItem['blog_id'] . "/" . SimpleUrl::toAscii($blogItem['blog_title']) .'">' . 
                    $blogItem['blog_title'] . '</a></li>';
            }
            echo "</ul>";
        }else{
            while($blogItem = mysql_fetch_array($blog)){
            ?>
            <div class="post">
                <h2 class="title"><a href="<?php echo $_SESSION['JAB_LINK'] . "blog/" . $blogItem['blog_id'] . "/" . SimpleUrl::toAscii($blogItem['blog_title']);?>"><?php echo $blogItem['blog_title'];?></a></h2>
                <p class="meta"><span class="date">The date implement</span><span class="posted">Posted by <a href="#">Someone</a></span></p>
                <div style="clear: both;">&nbsp;</div>
                <div class="entry">
                    <?php echo $blogItem['blog_content'];?>
                </div>
            </div>
            <?php
            }
        }
    }
}


推荐答案

如何调用 getLatestBlogsBig ?如果你在静态上下文中调用它( Blog :: getLatestBlogsBig()),那么 $ this t被解析成一个对象。您需要在 Blog 类的实例上调用 getLatestBlogsBig 方法。

How are you calling getLatestBlogsBig? If you're calling it in a static context (Blog::getLatestBlogsBig()), then $this can't be resolved into an object. You need to call the getLatestBlogsBig method on an instance of the Blog class.

这篇关于当不在对象上下文中时使用$ this的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 21:49