Fatal error: Using $this when not in object context

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sheppardzwc
Forum Newbie
Posts: 20
Joined: Mon Aug 17, 2009 3:04 pm

Fatal error: Using $this when not in object context

Post by sheppardzwc »

Hey everyone - again,

I'm having an issue with $this. I'm using it just how tutorials say we should use it and it's not accepting it. Any help?

Fatal error: Using $this when not in object context in C:\rk\includes\class.html.php on line 26

Code: Select all

<?php
 
####################
# RouteKanal, Ltd. #
####################
 
require_once('core.php');
 
class HTMLFactory {
    
    var $basic_content;
    var $create_content;
 
    private function createBasic($content) {
        $this->basic_content = $content;
        return $this->basic_content;
    }
        
    public function createPage($content) {
        $this->create_content = $content;
        echo(HTMLFactory::createBasic($this->create_content));
    }
    
}
 
HTMLFactory::createPage('test');
 
?>
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: Fatal error: Using $this when not in object context

Post by iankent »

You're calling the function as a static function on line 26, so $this within the function can't point to the object.

You need to create the object first, e.g.

Code: Select all

$h = new HTMLFactory();
$h->createPage('test');
hth
sheppardzwc
Forum Newbie
Posts: 20
Joined: Mon Aug 17, 2009 3:04 pm

Re: Fatal error: Using $this when not in object context

Post by sheppardzwc »

iankent wrote:You're calling the function as a static function on line 26, so $this within the function can't point to the object.

You need to create the object first, e.g.

Code: Select all

$h = new HTMLFactory();
$h->createPage('test');
hth
Alright, thanks.

Also - is "var $varname;" required to create the variable $this->varname or am I able to create it without the var declaration?
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: Fatal error: Using $this when not in object context

Post by iankent »

sheppardzwc wrote:Also - is "var $varname;" required to create the variable $this->varname or am I able to create it without the var declaration?
I'm not too sure whether its required, but I'd definately include it even if just for clarity.
sheppardzwc
Forum Newbie
Posts: 20
Joined: Mon Aug 17, 2009 3:04 pm

Re: Fatal error: Using $this when not in object context

Post by sheppardzwc »

iankent wrote:
sheppardzwc wrote:Also - is "var $varname;" required to create the variable $this->varname or am I able to create it without the var declaration?
I'm not too sure whether its required, but I'd definately include it even if just for clarity.
Alright. Thanks. :)
Post Reply