![](/img/trans.png)
[英]How to fix PHP Parse error: syntax error, unexpected '?', on laravel 5.8
[英]php fix parse error syntax error unexpected
为了防止错误信息渗透到系统中(例如,父亲比孩子小,或者父亲和孩子的姓氏不同),我们被要求设计两个名为 Person 和 Father 的类,用途:
$father = Father::firstName('Esaaro')->lastName('Ozaaraa')->age(42);
Person::firstName("Soobaasaa")->lastName( "Ozaaraa")->age(17)
->setFather( $father )-> toArray();
我已经退回了这个 output 但代码有一些问题。
$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 方法的输入值必须是 class 父亲的 object。
父亲和孩子的年龄必须至少为 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;
}
}
您的方法存在几个设计问题:
您不需要父亲 class。 它不能保证人 object 的年龄会小于父亲 object。 您的父亲 class 在数据方面没有任何不同的属性或规则。 您可以单独使用 Person class,并在setFather
方法中设置有关年龄的规则(更好的方法可能是在 Model 类之外进行验证,但这取决于您当前的程序架构)。
您的方法的命名也不清楚。 age()
设置您的 Person 实例的年龄,但firstName()
是 static 并创建一个新实例? 你为什么要调用$person = Person::firstname('Test');
而不是$person = new Person('Test');
? 我认为你不需要 static 方法,构造函数做同样的工作并且更合法。
你应该有适当的 getter 和 setter 来让你的 class 更容易理解。 像setAge()
和getAge()
。
您的toArray()
方法也很混乱。 这种方法应该做什么? 它的名称表明它将提供一个 Person 属性数组,但实际上您正在这里进行验证。
验证已经有错误值的 object 为时已晚。 当您调用toArray()
时,您已经有一个 Person object 充满了无效数据。 在尝试设置属性值时,必须先进行验证。 toArray()
应该只返回值,而不检查它们是什么。
我认为完成所有这些后,您应该会更加舒适地看到如何去做要求您做的事情:)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.