【发布时间】:2015-07-08 16:24:38
【问题描述】:
我有以下课程:
class Base {
static $static_var = 'base_static';
public static function static_init() {
// HERE, I want to get the caller extended class.
echo __CLASS__.'<br/>';
// HERE, I want to get the caller extended static variable.
echo static::$static_var.'<br/>';
// Do some initialization works depends on the static_var.
// ...
}
};
class Children extends Base {
// overridden
static $static_var = 'extended_static';
};
// Call Now
Children::static_init();
/** echos:
Base
base_static
*/
/** I want to export:
Children
extended_static
*/
我可以将 Base 类扩展到许多子类。
所以在我的子类中,我可以通过它们自己的静态变量来定义静态参数。
有没有办法做到这一点?或者我应该如何设计我的课程?
【问题讨论】: