Wierd Object Behavior

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
tiggyboo
Forum Newbie
Posts: 2
Joined: Sat Aug 24, 2002 8:49 am

Wierd Object Behavior

Post 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
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

When referring to the class variables use

Code: Select all

$this->varname
not

Code: Select all

$this->$varname
tiggyboo
Forum Newbie
Posts: 2
Joined: Sat Aug 24, 2002 8:49 am

Post by tiggyboo »

Thanks a bunch, could have sworn I'd tried that but obviously I didn't - works like a charm!

Thanks again,
Al
Post Reply