2

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
            }
        }
    }
}
akamike
  • 2,148
  • 13
  • 16
Ken
  • 2,654
  • 3
  • 26
  • 29
  • Please show the PHP code you are running to call the `Blog` class. – Pekka May 15 '10 at 15:49
  • Possible duplicate of [PHP Fatal error: Using $this when not in object context](http://stackoverflow.com/questions/2350937/php-fatal-error-using-this-when-not-in-object-context) – Alex Shesterov Apr 05 '17 at 19:32

2 Answers2

6

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.

Charles
  • 50,943
  • 13
  • 104
  • 142
1

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

Using $this->myFunction() inside a static function does not work. Use self::myFunction() instead. Just keep in mind that myFunction() must be a static function

Vidar Vestnes
  • 42,644
  • 28
  • 86
  • 100
  • It was problem with the static and none static class. Solved the problem using self:: now everything works thanks for the help – Ken May 15 '10 at 18:22