Builder - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of Builder.php

  1. @author rabzy<[email protected]>
  2.  
  3. <?php
  4.  
  5. class Profile
  6. {
  7.     private $profileBuilder;
  8.     private $firstName;
  9.     private $lastName;
  10.     private $age;
  11.     
  12.     public function __construct(ProfileBuilder $profileBuilder)
  13.     {
  14.         $this->profileBuilder = $profileBuilder;
  15.         
  16.         $this->firstName = $profileBuilder->getFirstName();
  17.         $this->lastName = $profileBuilder->getLastName();
  18.         $this->age = $profileBuilder->getAge();
  19.     }
  20. }
  21.  
  22.  
  23. class ProfileBuilder
  24. {
  25.     private $firstName;
  26.     private $lastName;
  27.     private $age;
  28.     
  29.     public function setFirstName(string $firstName): self
  30.     {
  31.         $this->firstName = $firstName;
  32.         
  33.         return $this;
  34.     }
  35.     
  36.     public function setLastName(string $lastName): self
  37.     {
  38.         $this->lastName = $lastName;
  39.         
  40.         return $this;
  41.     }
  42.     
  43.     public function setAge(int $age): self
  44.     {
  45.         $this->age = $age;
  46.         
  47.         return $this;
  48.     }
  49.     
  50.     public function getFirstName(): string
  51.     {
  52.         return $this->firstName;
  53.     }
  54.     
  55.     public function getLastName(): string
  56.     {
  57.         return $this->lastName;
  58.     }
  59.     
  60.     public function getAge(): int
  61.     {
  62.         return $this->age;
  63.     }
  64.     
  65.     public function build(): Profile
  66.     {
  67.         return new Profile($this);
  68.     }
  69. }
  70.  
  71. $pb = new ProfileBuilder();
  72.  
  73. $timon = $pb
  74.     ->setFirstName("Tymofii")
  75.     ->setLastName("Bubalo")
  76.     ->setAge(7)
  77.     ->build();
  78.  
  79. print_r($timon);
File Description
  • Builder
  • PHP Code
  • 22 Jan-2020
  • 1.42 Kb
You can Share it: