我正在学习 PHP 并阅读 Robin Nixon 的书。我在使用此代码时遇到问题:
<?php
class Centre
{
public $centre_name; // String: The name of the centre
public $tagline; // String: The centre's tagline
// Set the centres details. This will later be done through a form.
function set_details()
{
$this->centre_name = "YMCA";
$this->tagline = "Lets all go to the Y";
}
// Display the centres details.
function display()
{
echo "Centre Name - " . $centre->centre_name . "<br />";
echo "Centre Tagline - " . $centre->tagline . "<br />";
}
}
?>
<?php
$centre = new Centre();
$centre->set_details();
$centre->display();
?>
现在这是输出:中心名称 - 中心标语 - 因此正在设置变量。我在使用 $this->variable = "whatever"; 正确吗?