我是新來的PHP寫了這樣的代碼,包括類和兩個實例失敗。該類包含一個setter和getter方法來訪問私有title
屬性,每個實例顯示它,當所有的標題文字,被ucwords()
功能大寫。在這種情況下,它還包含一個「作者」屬性。二傳手的getter功能在PHP類
當我執行代碼時,我什麼都沒有(既不是title
也不是author
),也沒有任何錯誤,所以我不知道我做錯了什麼(我做它作爲個人練習的一部分在teamtreehouse.com學習時)。
class Recipe {
private $title;
public $author = "Me myself";
public function setTitle($title) {
echo $this->title = ucwords($title);
}
public function getTitle($title) {
echo $this->title;
}
}
$recipe1 = new Recipe();
$recipe1->getTitle("chinese tofu noodles");
$recipe1->author;
$recipe2 = new Recipe();
$recipe2->getTitle("japanese foto maki");
$recipe2->author = "A.B";
注:AFAIU從teamtreehous.com視頻,如果我們想訪問私有財產需要一個setter的getter功能。
爲什麼沒有任何印刷?