It should be obvious but the child method definition must match that of the parent, so you are missing an argument.
What I would do is this:
class Generic {
protected $a;
protected $b;
protected final function __construct($a,$b) {
$this->a = $a;
$this->b = $b;
echo $this->a." ".$this->b;
}
public static function create($a,$b) {
return new self($b,$a); //reverse arguments
}
}
class Wanter extends Generic {
public static function create($a, $b="I want") {
return parent::create($b,$a);
}
}
Note that I changed the order of the arguments, this way the one with the default is the second argument.
You could do this just in the child, but it might be somewhat confusing, if the order is different then the parent class.
That said, something like a factory method may be more appropriate in this instance.
https://en.wikipedia.org/wiki/Factory_(object-oriented_programming)
Besides a factory pattern I am not sure how important it is for you to have the static Create method. The constructor offers more flexibility when needing things like polymorphism. For example something like this would be acceptable
abstract class Generic {
protected $a;
protected $b;
protected function create($a,$b) {
$this->a = $a;
$this->b = $b;
echo $this->a." ".$this->b;
}
}
class Wanter extends Generic {
public function __construct($a) {
return $this->create("I want",$a);
}
}
Then each child can define it's own constructor, with its own set of required arguments.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…