Using $ this if not in an object context

I am creating a function to display a blog. So I created the show blog function, but it keeps giving the error "Using $ this if not in object context"

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
            }
        }
    }
}

      

+2


source to share


2 answers


How do you call getLatestBlogsBig

? If you call it in a static context ( Blog::getLatestBlogsBig()

) then $this

it cannot resolve into an object. You need to call a method getLatestBlogsBig

on an instance of the class Blog

.



+6


source


I don't understand how you can go to this error / line with the code you posted, since you have to be in object mode to reach this line. Is getLatestBlogsBig () declared static in the code you actually ran?



Using $this->myFunction()

inside a static function doesn't work. Use instead self::myFunction()

. Just keep in mind that myFunction () must be a static function

+1


source







All Articles