解决这个问题的最好方法是不要创建那个抽象类。
不要将对象创建与业务逻辑结合在同一个类中。这称为静态链接依赖,通常是有缺陷的设计,因为您无法在运行时更改它(例如,对于测试或每个挂钩)。Avoid inheritance.
Separation of concerns 和dependency injection 将解决这个问题。
实例
interface Plugin_Setup_Interface
{
public function activate( $network_wide );
public function deactivate( $network_wide );
}
class Plugin_Setup implements Plugin_Setup_Interface
{
public function activate( $network_wide ) {}
public function deactivate( $network_wide ) {}
}
class Plugin_Starter
{
private $plugin_file = \'\';
private $setup = NULL;
public function __construct(
$plugin_file,
Plugin_Setup_Interface $setup = NULL
)
{
$this->plugin_file = $plugin_file;
$this->setup = $setup;
}
public function init()
{
if ( ! is_null( $this->setup ) )
{
register_activation_hook(
$this->plugin_file,
array ( $this->setup, \'activate\' )
);
register_deactivation_hook(
$this->plugin_file,
array ( $this->setup, \'deactivate\' )
);
}
}
}
// call your auto-loader, then …
$start = new Plugin_Starter( __FILE__, new Plugin_Setup() );
$start->init();
现在,您可以为每个插件创建专门的去激活例程,并且仍然可以重用starter类。