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.
Result of php executing
Full code of Builder.php
- @author rabzy<[email protected]>
- <?php
- class Profile
- {
- private $profileBuilder;
- private $firstName;
- private $lastName;
- private $age;
- public function __construct(ProfileBuilder $profileBuilder)
- {
- $this->profileBuilder = $profileBuilder;
- $this->firstName = $profileBuilder->getFirstName();
- $this->lastName = $profileBuilder->getLastName();
- $this->age = $profileBuilder->getAge();
- }
- }
- class ProfileBuilder
- {
- private $firstName;
- private $lastName;
- private $age;
- public function setFirstName(string $firstName): self
- {
- $this->firstName = $firstName;
- return $this;
- }
- public function setLastName(string $lastName): self
- {
- $this->lastName = $lastName;
- return $this;
- }
- public function setAge(int $age): self
- {
- $this->age = $age;
- return $this;
- }
- public function getFirstName(): string
- {
- return $this->firstName;
- }
- public function getLastName(): string
- {
- return $this->lastName;
- }
- public function getAge(): int
- {
- return $this->age;
- }
- public function build(): Profile
- {
- return new Profile($this);
- }
- }
- $pb = new ProfileBuilder();
- $timon = $pb
- ->setFirstName("Tymofii")
- ->setLastName("Bubalo")
- ->setAge(7)
- ->build();
- print_r($timon);
File Description
- Builder
- PHP Code
- 22 Jan-2020
- 1.42 Kb
You can Share it:
Latest PHP Pastes