<分区>
Possible Duplicate:
Functionality of PHP get_class
对于小型 ORM-ish 类集,我有以下内容:
class Record {
//Implementation is simplified, details out of scope for this question.
static public function table() {
return strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', get_class()))."s";
}
static public function find($conditions) {
//... db-selection calls go here.
var_dump(self::table());
}
}
class Payment extends Record {
}
class Order extends Record {
public $id = 12;
public function payments() {
$this->payments = Payment::find(array('order_id', $this->id, '='));
}
}
$order = new Order();
$order->payments();
#=> string(7) "records"
我期望打印这段代码:
#=> string(8) "payments"
但是,相反,它打印records
。我试过 self::table()
,但结果相同。
编辑,在评论中的一些问题之后 table()
是一种简单地将类的名称映射到其对象所在的表的方法:Order
存在于orders
中,Payment
存在于payments
中; 记录
不存在!)。当我调用 Payments::find()
时,我希望它在表 payments
上搜索,而不是在表 records
上,也不在表 订单
。
我做错了什么?如何获取调用::的类的类名,而不是定义::的类的类名?
重要的部分可能是 get_class()
,无法返回正确的类名。