Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- declare(strict_types=1);
- class Car {
- private $brandName = "";
- private $doorsCount = 0;
- private $fuelType = "";
- private static $instance = null;
- public function __construct() {
- if (self::$instance === null) {
- self::$instance = $this;
- }
- }
- private static function getInstance() : Car { // Car or self instead (?) -> Both just works fine, but i don't know about its scope.
- return self::$instance;
- }
- public function getBrandName() : string {
- return $this->brandName;
- }
- public function setBrandName(string $brandName) : Car {
- $this->brandName = $brandName;
- return Car::getInstance();
- }
- public function getDoorsCount() : int {
- return $this->doorsCount;
- }
- public function setDoorsCount(int $count) : Car {
- $this->doorsCount = $count;
- return Car::getInstance();
- }
- public function getFuelType() : string {
- return $this->fuelType;
- }
- public function setFuelType(string $fuelType) : Car {
- $this->fuelType = $fuelType;
- return Car::getInstance();
- }
- public function __toString() : string {
- return "Car{brandName:'$this->brandName',doorsCount:'$this->doorsCount',fuelType:'$this->fuelType'}";
- }
- }
- $car = (new Car())
- ->setBrandName("Ferrari")
- ->setDoorsCount(2)
- ->setFuelType("Shell V-Performance");
- echo $car . PHP_EOL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement