php - php修复解析错误语法错误意外
问题描述
为了防止错误信息渗透到系统中(例如,父亲比孩子小,或者父亲和孩子的姓氏不同),我们被要求设计两个名为 Person 和 Father 的类,用途:
$father = Father::firstName('Esaaro')->lastName('Ozaaraa')->age(42);
Person::firstName("Soobaasaa")->lastName( "Ozaaraa")->age(17)
->setFather( $father )-> toArray();
我已返回此输出,但代码有一些问题。
$person = [
'firstName' => 'Soobaasaa',
'lastName' => 'Ozaaraa',
'age' => 17,
'father' => [
'firstName' => 'Esaaro',
'lastName' => 'Ozaaraa',
'age' => 42
]
];
详细考虑父亲和孩子的特征的条件是:
名字和姓氏必须是字符串类型,并且至少有 3 个字母,最多 15 个字母,并且不能包含数字字符。例如:“kaakero12”是不可接受的
孩子的年龄应该是 int 类型,从 1 到 130。(例如,数字 0 或 131 不能作为 age 方法的输入值。)
父亲必须至少 18 岁,最多 130 岁。(例如,数字 17 或 131 不能作为年龄方法的输入值。)
setFather 方法的输入值必须是Father 类的对象。
父亲和孩子的年龄必须至少为 18 岁。
注意父亲和孩子的年龄一定要确定(不老的孩子不能有父亲,反之亦然)
姓氏 LastName 父亲和孩子必须相同。
我的答案:
请帮助发现问题。
<?php
class Person
{
private $firstName;
private $lastName;
private $age;
private $father;
private function __construct(string $firstName) {
$this->firstName = $firstName;
}
public static function firstName(string $name = 'testhhhhhh') {
return new Person($name);
}
public function lastName(string $lastName) {
$this->lastName = $lastName;
return $this;
}
public function age(int $age) {
$this->age = $age;
return $this;
}
public function setFather(Father $father) {
$this->father = $father;
return $this;
}
public function toArray() {
$this->father->age
( (isset($this->firstName) && $this->firstName !== null && strlen($this->firstName) >= 3 && strlen($this->firstName) <= 15 && preg_match('/[0-9]/', $this->firstName) == false) ? $this->Person=['firstName'] == $this->firstName : '' );
( (isset($this->lastName) && $this->lastName !== null && strlen($this->lastName) >= 3 && strlen($this->lastName) <= 15 && preg_match('/[0-9]/', $this->lastName) == false) ? $this->Person=['lastName'] == $this->lastName : '' );
( (isset($this->age) && $this->age >= 1 && $this->age <=130 && preg_match('/[0-9]/', $this->age) == false) ? $this->Person=['age'] == $this->age : '' );
$this->Person=['father' => $this->father->toArray()];
}
}
class Father
{
protected static $name;
protected $family, $age, $result;
public static function firstName(string $name = null)
{
self::$name = $name;
}
public function lastName(string $lastName = null)
{
$this->family = $lastName;
}
public function age(string $age = null)
{
$this->age = $age;
}
public function toArray()
{
( (isset(static::$name) && static::$name !== null && strlen(static::$name) >= 3 && strlen(static::$name) <= 15) ? $this->result['firsName'] = self::$name : '' );
( (isset($this->family) && $this->family !== null && strlen($this->family) >= 3 && strlen($this->family) <= 15 && preg_match('/[0-9]/', $this->family) == false) ? $this->result['lastName'] = $this->family : '' );
( (isset($this->age) && $this->age !== null && $this->age >= 18 && $this->age <=130 && $this->age > (Person::age() + 18) ? $this->result['age'] = $this->age : '' );
return $this->result;
}
}
解决方案
您的方法存在几个设计问题:
你不需要父亲类。它不能保证 Person 对象的年龄会比 Father 对象年轻。您的父亲类在数据方面没有任何不同的属性或规则。您可以单独使用 Person 类,并在方法中设置有关年龄的规则setFather
(更好的方法可能是在 Model 类之外进行验证,但这取决于您当前的程序架构)。
您的方法的命名也不清楚。age()
设置您的 Person 实例的年龄,但firstName()
它是静态的并创建一个新实例?你为什么要打电话$person = Person::firstname('Test');
而不是$person = new Person('Test');
?我认为你不需要那个静态方法,构造函数做同样的工作并且更合法。
你应该有适当的 getter 和 setter 来让你的类更容易理解。喜欢setAge()
和getAge()
。
你的toArray()
方法也很乱。这种方法应该做什么?它的名称表明它将提供一个 Person 属性数组,但实际上您正在这里进行验证。
验证已经具有错误值的对象为时已晚。当您调用toArray()
时,您已经有一个充满无效数据的 Person 对象。在尝试设置属性值时,必须先进行验证。toArray()
应该只返回值,而不检查它们是什么。
我认为完成所有这些后,您应该会更加舒适地看到如何去做要求您做的事情:)。
推荐阅读
- java - 如何比较数组中的两个下一个随机值(Java)
- haskell - haskell 将 Integer 转换为 String 而不显示
- mysql - 无法访问根 phpmyadmin
- php - 删除 URL 尾部并仅保留域
- navigationbar - iOS 13 导航栏问题 - 部分导航栏变得透明
- flutter - 在 SQFlite 中数据库操作后刷新数据
- jquery - 引导崩溃无法在一台服务器上运行
- python - Tensorflow - 带有条件的掩码张量元素
- r - 在 stat_density 图中使用 facet_zoom
- android - Sharedpreference storeData 崩溃 android 应用