假设我有一个实体(一块石头),我有一些更具体的实体,它们扩展了基本的实体 - 例如,一个有说服力的石头,它有一些不同的属性和方法.石头的所有属性都存储在同一个表中的数据库中.特定对象的所有特定属性都序列化存储在表的"specific_options"字段中.我有"石头"课程和"Talking_Stone"课程,它扩展了基本的石头课程.我需要根据从数据库中获取的数据创建一个石头对象.
实现这样的事情的最佳做法是什么?
到目前为止我得到了什么:
class Stone_Data {
...
public static function get_item( $id ) {
$sql = 'SELECT * FROM `' . self::$table . '` WHERE `id`=' . ( int ) $id;
$item = self::$db->get_row( $sql );
return $item;
}
...
}
class Stone_Factory {
...
public static function create( $id = null ) {
// if the stone's type is set
if ( !is_null( $id ) && !is_null( $data = Stone_Data::get_item( $id ) ) && !empty( $data['type'] ) …Run Code Online (Sandbox Code Playgroud)