1
我有兩個類獲取包含對象
class Table {
public $rows = array();
public $name;
public function __construct($name, $rows) {
$this->name = $name;
$this->rows = $rows;
}
}
class Row {
public $name;
public function __construct($name) {
$this->name = $name;
}
}
現在我想創建一個對象表,並添加2行吧。
$rows = array(
new Row("Row 1"),
new Row("Row 2")
);
$table = new Table("Table 1", $rows);
到目前爲止好.. 但有可能得到一個排的含表? 例如:
foreach($table->rows AS $row) {
echo $row->name . ' is member of table ' . $row->getContainingTable()->name;
}
這僅僅是一個例子...
公共變量是一個壞主意,他們打破封裝。 – GordonM
我知道,但這只是一個示例代碼 – bernhardh