63417

Using $this when not in object context

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

Answer1:

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.

Answer2:

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

Recommend

  • Created a new variable by pulling a value from a MySQL table
  • MySQL query acting weird
  • Moving to mysqli, mysql_result
  • return variable argument as another value?
  • Using a join with three tables when a field might be null
  • Insert records into two table at once
  • Oracle - Second level subquery cannot see field from main query
  • Android: How to correctly use NotifyDataSetChanged with SimpleExpandableListAdapter?
  • Xaml, wpf image position and crop issue
  • Inversing an interpolation of rotation
  • Configure nginx to return different files to different authenticated users with the same URI
  • How to skip require in ruby?
  • Randomizing -and remembering that randomisation- multiple choice questions in php
  • Add dynamic data to line chart from mysql database with highcharts
  • Using Sax parsing to edit and write XML in VB6
  • Java color detection
  • Mysql - How to search for 26 records that each begins with the letter of the alphabet?
  • SharedPreferences or SQLite Database?
  • Limiting recursion to certain level - Duplicate rows
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • Uncaught Error: Could not find module `ember-load-initializers`
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • Perl system calls when running as another user using sudo
  • How to limit post in wp_query
  • Display Images one by one with next and previous functionality
  • Delete MySQLi record without showing the id in the URL
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to stop GridView from loading again when I press back button?
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Programmatically clearing map cache
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?