Page 1 of 1

Wierd Object Behavior

Posted: Sat Aug 24, 2002 8:49 am
by tiggyboo
Given the following object on say, page.inc:
<?
class Page
{
var $content;
var $title;

function SetTitle ($newtitle)
{
$this->$title = $newtitle;
}

function SetContent ($newcontent)
{
$this->$content = $newcontent;
}

function Display()
{
echo $this->$title;
echo $this->$content;
}
}
?>

Which is called by the following page:

<?
include "page.inc";

$homepage = new Page();

$homepage -> SetContent("general_content.php");

$homepage -> SetTitle("general_title.php");

$homepage -> Display();

?>

Why, when I run the 2nd page, is only "general_title.php" displayed when I echo the $homepage -> $content and $homepage -> $title via the $homepage -> Display() function? I.e., why is $homepage -> $content not resolving to "general_content.php" instead of "general_title.php"?

Thanks to anyone with the patience to hang with this :-)

- Al

Posted: Sat Aug 24, 2002 8:51 am
by nielsene
When referring to the class variables use

Code: Select all

$this->varname
not

Code: Select all

$this->$varname

Posted: Sat Aug 24, 2002 11:22 am
by tiggyboo
Thanks a bunch, could have sworn I'd tried that but obviously I didn't - works like a charm!

Thanks again,
Al