lundi 30 novembre 2020

Fatal error: Uncaught Error: Call to a member function lastName()

question ink: please show Details

Error:

Fatal error: Uncaught Error: Call to a member function lastName() on null in /home/mehdi/Desktop/solution.php:92 Stack trace: #0 {main} thrown in /home/mehdi/Desktop/solution.php on line 92

my answer with problem:

<?php

class Person
{
    private $firstName;
    private $lastName;
    private $age;
    private $father;

    private function __construct(string $firstName = null) {
        $this->firstName = $firstName;
    }

    public static function firstName(string $name = null) {
        return new Person($name);
    }

    public function lastName(string $lastName = null) {
        $this->lastName = $lastName;
        return $this;
    }

    public function age(int $age = null) {
        $this->age = $age;
        return $this;
    }

    public function setFather(Father $father = null) {
        $this->father = $father;
        return $this;
    }

    public function toArray() {

        if (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;
        }

        if (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;
        }

        if (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()
    {
        if (isset(static::$name) && static::$name !== null && strlen(static::$name) >= 3 && strlen(static::$name) <= 15){
            $this->result['firsName'] = self::$name;
        }

        if (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;
        }

        if (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;
    }
}

$father = Father::firstName('Esaaro')->lastName('Ozaaraa')->age(42);
Person::firstName("Soobaasaa")->lastName( "Ozaaraa")->age(17)
  ->setFather( $father )-> toArray();

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Aucun commentaire:

Enregistrer un commentaire