One way to avoid repeating all the parameters in the call of the __construct of an extended class:
class Alpha{
protected $a = '1';
protected $b = '2';
public function __construct($a, $b){
$this->a = $a;
$this->b = $b;
}
public function print_it(){
echo "a:'{$this->a}'\n";
echo "b:'{$this->b}'\n";
}
}
class BetaStandard extends Alpha{
public function __construct($a, $b){
parent::__construct($a, $b);
// do something else
}
}
class BetaLazy extends Alpha{
public function __construct(){
call_user_func_array(array('parent', '__construct'), func_get_args());
// do something else
}
}
$Bs = new BetaStandard('the a', 'the b');
$Bs->print_it();
$Bl = new BetaLazy('the a', 'the b');
$Bl->print_it();
No comments:
Post a Comment