1
我正在嘗試學習OOP並有幾個問題。我已閱讀了PHP Objects, Patterns, and Practice的前幾章,以及以下帖子; Nettuts+,PHP Freaks和PHPRO。Noob PHP OOP問題:構造函數和捲曲支架
- 在子類中,構造函數是否必須列出已經存在於父類中的變量?
- 當我在我的方法(或其他地方)中檢索一個屬性時,爲什麼我需要將我的值包裹在大括號中(即{$ this-> id})?另外,如果有人有任何建議(例如我做錯了什麼),我可以接受任何批評。
class Element {
public $tag;
public $id;
function __construct($tag, $id) {
$this->tag = $tag;
$this->id = $id;
}
public function getAttributes() {
return "id='{$this->id}'";
}
}
class NormalElement extends Element {
public $text;
function __construct($tag, $id, $text) {
parent::__construct($tag, $id);
$this->text = $text;
}
public function getElement() {
return "<{$this->tag}>{$this->text}</{$this->tag}>";
}
}
class VoidElement extends Element {
function __construct($tag, $id) {
parent::__construct($tag, $id);
}
public function getElement() {
return "<{$this->tag} " . parent::getAttributes() . " />";
}
}
我花了一段時間試圖讓我的代碼,在這篇文章中正確顯示,但它不斷地刷新。
哇......那是有史以來最詭異的經歷,試圖鱈魚在SO上格式化一個帖子...有關該有序列表的東西......必須拋出規則才能使其運行。 – prodigitalson 2012-03-06 05:34:08