Page 1 of 1

Fatal error: Using $this when not in object context

Posted: Thu Nov 26, 2009 8:43 am
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');
 
?>

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

Posted: Thu Nov 26, 2009 9:08 am
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

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

Posted: Thu Nov 26, 2009 9:18 am
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?

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

Posted: Thu Nov 26, 2009 9:39 am
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.

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

Posted: Thu Nov 26, 2009 9:51 am
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. :)