Solved

Fatal error: Using $this when not in object context

Posted on 2009-04-10
8
905 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 34

Accepted Solution

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

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

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 109

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 34

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 109

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
This article discusses how to create an extensible mechanism for linked drop downs.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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…

785 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