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
sheppardzwc
Forum Newbie
Posts: 20 Joined: Mon Aug 17, 2009 3:04 pm
Post
by sheppardzwc » Thu Nov 26, 2009 8:43 am
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');
?>
iankent
Forum Contributor
Posts: 333 Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom
Post
by iankent » Thu Nov 26, 2009 9:08 am
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
Post
by sheppardzwc » Thu Nov 26, 2009 9:18 am
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?
iankent
Forum Contributor
Posts: 333 Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom
Post
by iankent » Thu Nov 26, 2009 9:39 am
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
Post
by sheppardzwc » Thu Nov 26, 2009 9:51 am
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.