我编写了一个父类(使用后期静态绑定),从中继承了我的数据库类。我正在尝试编写一个构造函数来将每个表列分配为子类的公共属性。到目前为止,我可以在子类中编写构造函数,效果很好,但我想将它放在父类中,以便自动定义所有子类属性。这是我的孩子班:
class Sale extends DatabaseObject {
protected static $table_name="invoices";
protected static $db_fields = array();
function __construct() {
global $database;
$query_cols = " SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME LIKE '" . self::$table_name . "' AND TABLE_SCHEMA LIKE '" . DB_NAME . "' ORDER BY ORDINAL_POSITION ASC";
$cols = $database->query($query_cols);
while($col = $database->fetch_array($cols)) {
if(!property_exists($this,$col['COLUMN_NAME'])) {
$this->$col['COLUMN_NAME']=NULL;
array_push(self::$db_fields,$col['COLUMN_NAME']);
}
}
}
}
要在父级中使用此构造函数,我需要能够定义被调用的类属性。
function __construct() {
$class_name = get_called_class();
$query_cols = " SELECT COLUMN_NAME FROM information_schema WHERE TABLE_NAME LIKE '" . static::$table_name . "' AND TABLE_SCHEMA LIKE '" . DB_NAME . "'";
$query_cols .= " ORDER BY ORDINAL_POSITION ASC";
$cols = $database->query($query_cols);
while($col = $database->fetch_array($cols)) {
if(!property_exists($class_name,$col['COLUMN_NAME'])) {
// the code to define called class public property?!
}
}
}
提前致谢。