簡體   English   中英

php 修復解析錯誤語法錯誤意外

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM