?
Solved

Fatal error: Using $this when not in object context

Posted on 2009-04-10
8
Medium Priority
?
977 Views
Last Modified: 2012-05-06
Hi,

I'm currently using the code below to try and post a test topic on my forum using the Invision Power Board API, however I keep getting the EM..

Fatal error: Using $this when not in object context in /home/smacktalks/public_html/forums/post.php on line 6

I read that it's due to using $this outside of a class, however this is the first time I have used API and don't know how to correct this. Would someone be able to amend my code to show me how this is done?

Many thanks,

Paul.
<?php
 
include_once("sources/api/api_topics_and_posts.php");
 
$api = new api_topics_and_posts();
$api->ipsclass = &$this->ipsclass;
$api->set_author_by_name("Paul"); /* Use the actual member name not id */
$api->set_post_content("Hello test post."); /* Post */
$api->set_forum_id("37"); /* The forum ID to post in */
$api->set_topic_title("Pauls test post"); /* The topic title */
$api->set_topic_description("Testing"); /* The topic description */
$api->set_topic_state('open'); /* The topic state 'open' or 'closes' to choose from */
$api->create_new_topic();
$this->ipsclass->print->redirect_screen("Redirecting to topic now", "showtopic={$api->topic_id}" );
 
?>

Open in new window

0
Comment
Question by:m0tSiE
  • 3
  • 3
  • 2
8 Comments
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 1000 total points
ID: 24117245
Try changing $this to $api?

It's hard to say for sure unless we know the class definitions.
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 total points
ID: 24117731
$this is a reserved word (a pseudo-variable) in PHP and always causes an error when used outside a Class.

http://us3.php.net/manual/en/language.oop5.basic.php

Tell us what you are meaning to achieve with that last line up there, OK?  Thanks, ~Ray
0
 

Author Comment

by:m0tSiE
ID: 24117967
I've changed

$api->ipsclass = &$this->ipsclass;

to

$api->ipsclass = $api->ipsclass;

Now it seems to import the sources/api/api_topics_and_posts.php file, but it now gives me the error

Fatal error: Call to a member function build_and_exec_query() on a non-object in /home/smacktalks/public_html/forums/sources/api/api_topics_and_posts.php on line 593

I checked the file and it reffered to my code line of "$api->set_author_by_name("Paul");", so I set this to $api->set_author_by_id("1"); and it still gave me the same kind of error, but this time refferenced the lines shown below.

I know the user id is correct, but I dunno what else could be causing this.
		$this->author = $this->ipsclass->DB->build_and_exec_query( array( 'select' => '*',
																		  'from'   => 'members',
																		  'where'  => "id=".intval($id),
																		  'limit'  => array( 0, 1 ) ) );
																		  
		if ( ! $this->author['id'] )
		{
			$this->api_error[] = 'no_user_found';
			$this->author      = array();
			return FALSE;
		}
		
		return TRUE;
	}

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24118073
$this with or without an ampersand in front of it does not make sense outside a class.  You might want to step back a bit and get some foundation in PHP and OOP.  If you are familiar with OOP in other languages, that is helpful, but PHP does some things differently.

Here is a good learning link:
http://www.phpdeveloper.org/news/5719

In the code snippet is an example of using OOP in PHP5

HTH, ~Ray
<?php // RAY_oop_vars.php
error_reporting(E_ALL);
 
// DECLARE THE CLASS
Class MyOOP
{
   public  $my_public_var  = 'open';
   private $my_privat_var;
 
// DECLARE THE CONSTRUCTOR
   public function __construct()
   {
      $this->my_privat_var = 'closed';
   }
 
// DECLARE A FUNCTION
   public function show_vars()
   {
      echo "<br/>\n";
      echo "<br/>PUBLIC $this->my_public_var \n";
      echo "<br/>PRIVAT $this->my_privat_var \n";
   }
 
// DECLARE ANOTHER FUNCTION
   public function set_privat($value)
   {
      $this->my_privat_var = $value;
   }
// END CLASS DECLARATION
}
 
 
 
 
// INSTANTIATE THE CLASS AND SHOW THE INITIAL VALUES OF VARIABLES
$a = new MyOOP;
$a->show_vars();
 
// SET A PRIVATE VARIABLE VIA THE FUNCTION
$a->set_privat('NewValue');
 
// SET A PUBLIC VARIABLE DIRECTLY
$a->my_public_var = 'Cheese';
 
// SEE THE NEW VALUES
$a->show_vars();
 
// DUMP THE VALUES FROM THE OBJECT
echo "<pre>";
var_dump($a);
 
// TRY TO SET A PRIVATE VARIABLE THE WRONG WAY
$a->my_privat_var = 'Beer';

Open in new window

0
 

Author Closing Comment

by:m0tSiE
ID: 31568967
Cheers Guys!
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 24118953
I agree with Ray - it would be very valuable to spend some time reading about object-oriented programming. Otherwise you might end up spending the same or greater amount of time asking questions on EE but you won't get as much out of it.

(I don't want to discourage you from asking questions on EE, of course, I'm just trying to suggest what might benefit you most at this moment.)
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24120079
Thanks for the points and I would add to gr8gonzo's last comment that a great place to begin reading on PHP's OOP capabilities is here.  As always, the user-contributed notes are the most inspired part of the documentation!

http://us2.php.net/manual/en/language.oop5.php

Best to all, ~Ray
0
 

Author Comment

by:m0tSiE
ID: 24120087
Thanks guys, i'm gonna check them out. :)
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question