Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5.  
  6. class Car {
  7. private $brandName = "";
  8. private $doorsCount = 0;
  9. private $fuelType = "";
  10.  
  11. private static $instance = null;
  12.  
  13. public function __construct() {
  14. if (self::$instance === null) {
  15. self::$instance = $this;
  16. }
  17. }
  18.  
  19. private static function getInstance() : Car { // Car or self instead (?) -> Both just works fine, but i don't know about its scope.
  20. return self::$instance;
  21. }
  22.  
  23. public function getBrandName() : string {
  24. return $this->brandName;
  25. }
  26.  
  27. public function setBrandName(string $brandName) : Car {
  28. $this->brandName = $brandName;
  29. return Car::getInstance();
  30. }
  31.  
  32. public function getDoorsCount() : int {
  33. return $this->doorsCount;
  34. }
  35.  
  36. public function setDoorsCount(int $count) : Car {
  37. $this->doorsCount = $count;
  38. return Car::getInstance();
  39. }
  40.  
  41. public function getFuelType() : string {
  42. return $this->fuelType;
  43. }
  44.  
  45. public function setFuelType(string $fuelType) : Car {
  46. $this->fuelType = $fuelType;
  47. return Car::getInstance();
  48. }
  49.  
  50. public function __toString() : string {
  51. return "Car{brandName:'$this->brandName',doorsCount:'$this->doorsCount',fuelType:'$this->fuelType'}";
  52. }
  53. }
  54.  
  55. $car = (new Car())
  56. ->setBrandName("Ferrari")
  57. ->setDoorsCount(2)
  58. ->setFuelType("Shell V-Performance");
  59.  
  60. echo $car . PHP_EOL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement