php$this->;函数中的变量不能像我希望的那样工作
我正在学习PHP,正在阅读罗宾·尼克松的一本书。我在使用此代码时遇到问题:php$this->;函数中的变量不能像我希望的那样工作,php,variables,this,Php,Variables,This,我正在学习PHP,正在阅读罗宾·尼克松的一本书。我在使用此代码时遇到问题: <?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
<?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();
?>
更改此选项
function display()
{
echo "Centre Name - " . $centre->centre_name . "<br />";
echo "Centre Tagline - " . $centre->tagline . "<br />";
}
函数显示()
{
回显“中心名称-”$center->中心名称。“
”;
呼应“中心标语-”$center->标语。“
”;
}
到
函数显示()
{
回显“中心名称-”$this->Centre_Name.“
”;
呼应“中心标语-”$this->Tagline.“
”;
}
您使用了$center
而不是$this
更改此设置
function display()
{
echo "Centre Name - " . $centre->centre_name . "<br />";
echo "Centre Tagline - " . $centre->tagline . "<br />";
}
函数显示()
{
回显“中心名称-”$center->中心名称。“
”;
呼应“中心标语-”$center->标语。“
”;
}
到
函数显示()
{
回显“中心名称-”$this->Centre_Name.“
”;
呼应“中心标语-”$this->Tagline.“
”;
}
您使用了$center
而不是$this
将函数dispaly()更改如下:
在类中,您可以使用$this
function display()
{
echo "Centre Name - " . $this->centre_name . "<br />";
echo "Centre Tagline - " . $this->tagline . "<br />";
}
函数显示()
{
回显“中心名称-”$this->Centre_Name.“
”;
呼应“中心标语-”$this->Tagline.“
”;
}
如下更改函数dispaly():
在类中,您可以使用$this
function display()
{
echo "Centre Name - " . $this->centre_name . "<br />";
echo "Centre Tagline - " . $this->tagline . "<br />";
}
函数显示()
{
回显“中心名称-”$this->Centre_Name.“
”;
呼应“中心标语-”$this->Tagline.“
”;
}
可以,但在display()中将“center”替换为“this”:
函数显示()
{
回显“中心名称-”$this->Centre_Name.“
”;
呼应“中心标语-”$this->Tagline.“
”;
}
可以,但在display()中将“center”替换为“this”:
函数显示()
{
回显“中心名称-”$this->Centre_Name.“
”;
呼应“中心标语-”$this->Tagline.“
”;
}
您的显示
功能必须使用$this
而不是$center
。您的显示
功能必须使用$this
而不是$center
。将$thi->tagline
更改为$this->tagline
更改为$this->tagline
。。。我怎么会错过呢。非常感谢。@whataboutki欢迎。:)啊。。。我怎么会错过呢。非常感谢。@whataboutki欢迎。:)