2 Replies - 584 Views - Last Post: 27 August 2009 - 10:34 PM Rate Topic: -----

#1 TechWar  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 470
  • Joined: 17-February 09

Need help with '$this'

Post icon  Posted 27 August 2009 - 09:38 PM

This is the error I get and I can't figure out why, because I am using it inside a function.
BTW: this is not life of death, I'm just playing with some code. :)


Fatal error: Using $this when not in object context in K:\business\catalogue.php on line 22



<?php
session_start();
include('../_site_config.php');

//Business tier class for reading product catalog information
class Catalog{
	public $lists;
	//Retrieves all pic_types
	public static function getPicTypes(){
		//build SQL query
		$sql = "SELECT pic_type FROM pictures";
		
		//Prepare the statement with PDO-specific functionality
		$result=DatabaseHandler::Prepare($sql);
		
		//Execute the query
		return DatabaseHandler::GetAll($result);
	}
	
	public static function listPicTypes(){
		//make the to display pic_types
		$this->lists=Catalog::getPicTypes();
		
		for($i=0;$i<count($lists);$i++){
			 echo "<li>".$lists[$i]['pic_type']."</li>";
		}// end for loop
		
	}// end listPicTypes function
		
		
}//end Catalog class

?>



Is This A Good Question/Topic? 0
  • +

Replies To: Need help with '$this'

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4436
  • View blog
  • Posts: 12,307
  • Joined: 18-April 07

Re: Need help with '$this'

Posted 27 August 2009 - 10:31 PM

Remember that to use $this you are saying to use a variable relevant to THIS instance of the object. The problem here is that you have declared your methods to be STATIC which means that you don't need instances of the object to call the methods. So the question is, how do you use an instance variable when you can call the method without an instance?

Remove your static keywords in other words. Unless you are attempting to use those functions without needing to define an instance of the class (which in this situation appears not to be the case) they should not be static.

This post has been edited by Martyr2: 27 August 2009 - 10:32 PM

Was This Post Helpful? 2
  • +
  • -

#3 TechWar  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 470
  • Joined: 17-February 09

Re: Need help with '$this'

Posted 27 August 2009 - 10:34 PM

Awesome, thanks again Martyr2! This worked perfectly without the '$this' in it at all, I just wanted to play some :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1