我正在尝试创建一个Developer
作为Person
.
我希望他们都使用静态工厂模式(或“命名构造函数”)。
我见过这种模式的一些例子,但没有一个使用继承。
问题 1
在示例中,他们使构造函数方法始终为私有。
是否可以对其进行保护以便从子构造函数中调用?
或者我应该解决使构造函数始终私有的问题,并尝试构建create
从子create
方法调用父方法的继承?
问题 2
当我尝试实例化 Person 或 Developer 类时,我收到以下错误。为什么?
PHP Fatal error: Declaration of Developer::create(string $name, string $surname, ?int $yearsOfExperience = NULL, ?string $preferredLanguage = NULL): Developer must be compatible with Person::create(string $name, string $surname): Person in InheritanceTest.php on line 57
当我删除这: self
两种方法中的类型提示时它会起作用create
,但我不明白为什么它们不兼容,如果Developer
是Person
.
提前致谢。
<?php
class Person
{
protected $name;
protected $surname;
protected function __construct(string $name, string $surname)
{
$this->name = $name;
$this->surname = $surname;
}
public static function create(string $name, string $surname): self
{
// Some validation
if($name == ''){
throw new InvalidArgumentException('A person name can not be empty.');
}
if($surname == ''){
throw new InvalidArgumentException('A person surname can not be empty.');
}
return new self($name, $surname);
}
}
class Developer extends Person
{
protected $yearsOfExperience;
protected $preferredLanguage;
protected function __construct(string $name, string $surname, ?int $yearsOfExperience, ?string $preferredLanguage)
{
parent::__construct($name, $surname);
$this->yearsOfExperience = $yearsOfExperience;
$this->preferredLanguage = $preferredLanguage;
}
public static function create(string $name, string $surname, ?int $yearsOfExperience = null, ?string $preferredLanguage = null): self
{
// Some validation
if($yearsOfExperience < 0){
throw new InvalidArgumentException('The years of experience can not be negative.');
}
if($preferredLanguage == ''){
throw new InvalidArgumentException('The preferred language can not be empty.');
}
return new self($name, $surname, $yearsOfExperience, $preferredLanguage);
}
}