-2

I'm trying to get this result on my php code, I need the implementation of

class person 

    $person = new person();

    $person->setFirstName('name')
        ->setLastName('lastname')
        ->setEmail('email@example.com')
    ;

    echo $user;

and then i have this result Will result a string

"name Lastname "

IS the example of my class implementation but it didn't work I need to implement three setters setFirstName,setLastName,setEmail to get my result code above .

class User {
  private $FirstName;
  private $LastName;
  private $Email;
    public function getFirstName() {
        return $this->FirstName;
    }

    public function setFirstName($x) {
        $this->FirstName = $x;
    }

    public function getLastName() {
        return $this->LastName;
    }

    public function setLastName($x) {
        $this->LastName = $x;
    }

    public function getEmail() {
        return $this->Email;
    }

    public function setEmail($x ) {
        $this->Email = $x;
    }
}
Samir Guiderk
  • 187
  • 2
  • 4
  • 17
  • 1
    Where does `$user` come from and where is what you have tried? Perhaps the [manual](http://php.net/manual/en/language.oop5.basic.php) will help you understand how to do this. – Jaquarh Jan 08 '19 at 19:29
  • You can check https://stackoverflow.com/questions/3724112/php-method-chaining to learn about method chaining (hint: create the functions in the class that will return `$this`), then you can also create a get function to get one or all of the properties. – aynber Jan 08 '19 at 19:34

2 Answers2

2

Your question is hard to understand. Anyways this code will provide you a class Person and test it. Output: "name lastname <email@example.com>"

Code:

    class Person
    {
        private $firstname, $lastname, $email;

        function setFirstName($firstname) {
            $this->firstname = $firstname;
            return $this;
        }

        function setLastName($lastname) {
            $this->lastname = $lastname;
            return $this;
        }

        function setEmail($email) {
            $this->email = $email;
            return $this;
        }

        function __toString() {
            return $this->firstname. ' ' .$this->lastname. ' &lt;' . $this->email .'&gt;';
        }
    }

    $person = new Person();

    $person->setFirstName('name')
        ->setLastName('lastname')
        ->setEmail('email@example.com');

    echo $person;

Hope this is what you searched for! I think what you need is the & gt; and & lt;

Updater
  • 459
  • 2
  • 13
1

If I understand your answer correctly, you want the following:

echo $person->getFirstName() . ' ' . $person->getLastName() . ' ' . $person->getEmail();

Resulting in: Samir Guiderk Samir@example.com>

lucid
  • 422
  • 3
  • 15
  • He wants you to write the code for him, ie `class Person { ... }` do not answer to off-topic questions. – Jaquarh Jan 08 '19 at 19:36