确保始终构造父类的最佳方法是什么,因为我们可以轻松地覆盖构造函数而无需调用它们。
另外:这是不好的做法吗?
abstract class A
{
// make it final to ensure parent constructor is allways used
final function __construct($param1)
{
// do essencial stuff here with $param1
// pass construction with remaining args to child
call_user_func_array(array($this,'init'),array_slice(func_get_args(),1));
}
abstract function init();
}
class B extends A
{
function init($param2)
{
// effectively extends parent constructor, no?
}
}
$obj = new B("p1","p2");