Reputation: 47
Here is a trivial class with a method that should return the string that has been assigned to the property "question". Why does it not print the returned property value from the method output?
I get no error messages, all I get is "Here is:" but the property's value is missing :(
class DisplayQuestion {
public $question;
function __construct ($question){
$this->question = $question;
}
function output(){
echo "<p>Here is: $this->question</p>";
}
}
$test = new DisplayQuestion("What's your question?");
$test->output();
Upvotes: 0
Views: 81
Reputation: 1029
Try this:
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();
If you get "Here is: bug test", then your PHP version is less than 5 on your development server. In PHP 4, __construct is not recognized as a constructor so you would have to replace it with the following:
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();
Try to determine for sure which PHP version you have by running phpinfo() on your server.
Upvotes: 0
Reputation: 18290
I run that code perfectly well on my machine, which means there is another issue (it's not the code). Check your PHP logs as well as your HTTP server's error and access logs, and (on your development server) enable display_errors in your ini file and see what's going on.
Upvotes: 1