vsko
4/11/2017 - 1:01 PM

Simple Factory pattern

Simple Factory pattern

<?php

interface Door {
    public function getWidth(): float;
    public function getHeight(): float;
}

class WoodenDoor implements Door {
    protected $width;
    protected $height;
    
    public function __construct(float $width, float $height) {
        $this->width = $width;
        $this->height = $height;
    }
    
    public function getWidth(): float {
        return $this->width;
    }
    
    public function getHeight(): float {
        return $this->height;
    }
}

class DoorFactory extends WoodenDoor {
    public static function makeDoor($width, $height): Door {
        return new WoodenDoor($width, $height);
    }
}

$door = DoorFactory::makeDoor(150, 400);
echo 'Width: ' . $door->getWidth();
echo "\r\n";
echo 'Height: ' . $door->getHeight();