Solved

Fatal error: Using $this when not in object context

Posted on 2009-04-10
8
912 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

820 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