php 父类中获取子类的名称,php – 在父类中获取子类的名称(静态上下文)

我正在构建一个重用和简单的ORM库;一切都很好,除了我被一个愚蠢的继承限制。请考虑以下代码:

class BaseModel {

/*

* Return an instance of a Model from the database.

*/

static public function get (/* varargs */) {

// 1. Notice we want an instance of User

$class = get_class(parent); // value: bool(false)

$class = get_class(self); // value: bool(false)

$class = get_class(); // value: string(9) "BaseModel"

$class = __CLASS__; // value: string(9) "BaseModel"

// 2. Query the database with id

$row = get_row_from_db_as_array(func_get_args());

// 3. Return the filled instance

$obj = new $class();

$obj->data = $row;

return $obj;

}

}

class User extends BaseModel {

protected $table = 'users';

protected $fields = array('id', 'name');

protected $primary_keys = array('id');

}

class Section extends BaseModel {

// [...]

}

$my_user = User::get(3);

$my_user->name = 'Jean';

$other_user = User::get(24);

$other_user->name = 'Paul';

$my_user->save();

$other_user->save();

$my_section = Section::get('apropos');

$my_section->delete();

显然,这不是我期待的行为(虽然实际行为也有意义)..所以我的问题是,如果你们知道一个意思,在父类,孩子类的名称。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值