-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolid.factory.php
65 lines (49 loc) · 1.2 KB
/
solid.factory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
interface iAnimal {
public static function respirar();
}
abstract class Animal implements iAnimal{
function greeting() {
$sound = $this->sound(); // exists in child class by contract
return strtoupper($sound);
}
abstract function sound(); // this is the contract
abstract static function respirar();
}
class Dog extends Animal {
function sound() { // concrete implementation is mandatory
return "Woof!";
}
static function respirar(){ echo 'respirando';}
}
class Cat extends Animal {
function sound() { // concrete implementation is mandatory
return "miauuuu!";
}
static function respirar(){ echo 'respirando';}
}
class Config {
private $animalType;
function __construct(){
$this->animalType='Cat';
}
public function getAnimalType(){
return $this->animalType;
}
}
class Maker {
static function Make($config){
$ClasseAnimal = $config->getAnimalType();
$animal = new $ClasseAnimal();
return $animal;
}
}
$dog = new Dog();
echo $dog->greeting(); // WOOF!
//$dog->respirar();
Dog::respirar();
echo "\n";
$conf = new Config();
$ani = Maker::Make($conf);
echo $ani->sound();
?>