Solved

Fatal error: Using $this when not in object context

Posted on 2009-04-10
8
897 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 108

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
 
LVL 108

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 108

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
This article discusses how to create an extensible mechanism for linked drop downs.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now