下面是我一直在尝试的一些代码,我已经搞砸了包括die()和echo()在这里和那里,但没有任何反应,如果我尝试运行别人的类代码它工作正常的对象的实例,但我看不出我的代码有什么问题。
class Form {
private $form_method;
public function __construct()
{
$form_method = '';
}
public function setFormMethod($method)
{
$this->$form_method=$method;
}
public function getFormMethod()
{
return $this->$form_method;
}
}
$newForm = new Form();
$newForm->setFormMethod('POST');
$var = getFormMethod();
echo $var;
我顺便通过XAMPP在本地主机上运行PHP 5.3.2。
任何帮助表示赞赏
答案 0 :(得分:3)
我看到了一些事情。首先,当您使用“this”引用它们时,不应该使用变量名称包含$。
示例:更改
return $this->$form_method;
要
return $this->form_method;
对getFormMethod()的调用也不起作用,因为代码引用了类外的方法。
更改
$var = getFormMethod();
要
$var = $newForm->getFormMethod();
正如Catfish建议的那样,你可能也会错过php标签(除非你刚发布了一个摘录)
此代码应该可以使用(未经测试)
<?php
class Form {
private $form_method;
public function __construct()
{
$this->form_method = '';
}
public function setFormMethod($method)
{
$this->form_method = $method;
}
public function getFormMethod()
{
return $this->form_method;
}
}
$newForm = new Form();
$newForm->setFormMethod('POST');
$var = $newForm->getFormMethod();
echo $var;
?>