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