OOP PHP 不起作用,网页不返回任何内容>



我正在尝试我的第一个OOP PHP,但我似乎遇到了麻烦。据我所见,代码没有任何问题,也没有收到错误消息。已经谢谢了!

<?PHP
class President {
    public $lastname;
    public $dob;
    public $dod;
    public $firstlady;
    public $wikipage;
    public $display;
    public $party;
    public $inoffice;
    public function __construct ($name, $dob, $dod, $girl, $wiki, $pic, $party, $terms){
        $this->lastname = $name;
        $this->dob = $dob;
        $this->dod = $dod;
        $this->firstlady = $girl;
        $this->wikipage = $wiki; 
        $this->display = $pic;
        $this->party = $party;
        $this->inoffice = $terms;
    }
    public function Write(){
        return "President". $name . "was in office for" . $terms . "." . "He was born on" . $dob . "and" . $dod . "." . "He represented the" . $party . "and lived in the Whitehouse with his wife" . 
        $girl . "<br/>" . "<img src'" . $pic . "' />" . "<br />" . "<a href'" . $wiki . "'> Read more on Wikipedia</a>";
    }
}
$obama = new President();
$obama->Write('Obama', 'June First 1992', 'is still alive', 'Michelle Obama', 'http://www.google.com', 'www.google.com/', 'Democrat', 'Two Terms');
echo $obama;
?>

首先,打开错误报告。。。然后

我能看到的两个问题。

首先,构造函数需要很多参数,在实例化对象时不会传递这些参数,而是在调用Write方法时传递这些参数。

然后,当$obama->Write(..)返回一个字符串时,您不会回显它。

解决方案:

$obama = new President('Obama', 'June First 1992', 'is still alive', 'Michelle Obama', 'http://www.google.com', 'www.google.com/', 'Democrat', 'Two Terms');
echo $obama->Write();

假设您的参数在您的构造中是正确的。

编辑

正如下面的注释中所述,您的write方法将无法访问任何类vars,因为它只查看本地范围。你需要像这样更改你的变量调用:

return "President". $name

return "President". $this->name

相关内容

最新更新