Php 为什么该方法不返回属性值?

Php 为什么该方法不返回属性值?,php,oop,Php,Oop,下面是一个简单的类,其方法应返回已分配给属性“question”的字符串。为什么不打印方法输出返回的属性值 我没有收到任何错误消息,只收到“Here is:”但缺少属性的值:( 类显示问题{ 公众提问; 函数构造($question){ $this->question=$question; } 函数输出(){ echo“这里是:$this->question”; } } $test=newdisplayquestion(“你的问题是什么?”); $test->output(); 我在

下面是一个简单的类,其方法应返回已分配给属性“question”的字符串。为什么不打印方法输出返回的属性值

我没有收到任何错误消息,只收到“Here is:”但缺少属性的值:(

类显示问题{
公众提问;
函数构造($question){
$this->question=$question;
}   
函数输出(){
echo“这里是:$this->question

”; } } $test=newdisplayquestion(“你的问题是什么?”); $test->output();
我在我的机器上很好地运行了该代码,这意味着还有另一个问题(不是代码)。请检查PHP日志以及HTTP服务器的错误和访问日志,并且(在开发服务器上)在ini文件中启用显示错误,并查看发生了什么情况。

尝试以下操作:

class DisplayQuestion {
    public $question = "bug test";

    function __construct ($question){
        $this->question = $question;
    }   

    function output(){
        echo "<p>Here is: $this->question</p>";         
    }
}   
$test = new DisplayQuestion("What's your question?");
$test->output();
类显示问题{
public$question=“bug测试”;
函数构造($question){
$this->question=$question;
}   
函数输出(){
echo“这里是:$this->question

”; } } $test=newdisplayquestion(“你的问题是什么?”); $test->output();
如果您得到“There is:bug test”,那么您的开发服务器上的PHP版本低于5。在PHP 4中,_构造不被识别为构造函数,因此您必须用以下内容替换它:

class DisplayQuestion {
    var $question;

    function DisplayQuestion ($question){
        $this->question = $question;
    }   

    function output(){
        echo "<p>Here is: $this->question</p>";         
    }
}   
$test = new DisplayQuestion("What's your question?");
$test->output();
类显示问题{
var$问题;
函数DisplayQuestion($question){
$this->question=$question;
}   
函数输出(){
echo“这里是:$this->question

”; } } $test=newdisplayquestion(“你的问题是什么?”); $test->output();

通过在服务器上运行phpinfo()来确定您的PHP版本。

错误日志中没有任何内容:(我得到的只是“Here is:”但是属性的值丢失了,这就是为什么我认为语法有问题的原因。所以,你的问题说它是一个空白页…这是重要的信息;特别是因为有两个其他的答案被删除了,考虑到这些新信息,这两个答案可能是准确的。如果有帮助的话,我很高兴取消删除我的答案s OP.I不是非常自信,因为我认为如果他有一个不支持该语法的PHP版本,他会得到一个语法错误(文档清楚地说明它是有效的。也许上面的答案是(re:PHP4)是准确的。真的很抱歉。我的意思是属性为空。我尝试了@deefour的语法,但它也不起作用。请编辑此问题,以指示下面所示的完整输出。您使用的是什么版本的PHP?PHP5.2,应该可以,对吗?
$test=new DisplayQuestion(“您的问题是什么?”);echo“”.$test->question.

”;
输出正确吗?不,这也不行。PHP4版本语法不正确;PHP4中不支持变量范围。
public$question
应该是
var$question
。你完全正确,我错过了。谢谢你指出这一点。
class DisplayQuestion {
    var $question;

    function DisplayQuestion ($question){
        $this->question = $question;
    }   

    function output(){
        echo "<p>Here is: $this->question</p>";         
    }
}   
$test = new DisplayQuestion("What's your question?");
$test->output();