[英]OOP PHP not working, the web page returns nothing>
我正在嘗試我的第一個 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;
?>
首先,打開錯誤報告...然后...
我能看到的2個問題。
首先,你的構造函數需要很多參數,當你實例化對象時你沒有傳遞這些參數,但是當你調用Write
方法時,它並不期待任何東西。
然后,當它返回一個字符串時,你不要 echo $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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.