Solved

Fatal error: Using $this when not in object context

Posted on 2009-04-10
8
917 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 35

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 110

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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 110

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 110

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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 the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

738 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