-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsolid_01_srp_refactored.php
More file actions
53 lines (41 loc) · 1.1 KB
/
solid_01_srp_refactored.php
File metadata and controls
53 lines (41 loc) · 1.1 KB
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
<?php
/*
SRP The Single Responsibility Principle
A class should have one, and only one, reason to change.
### Refactored Code ###
*/
class Hero{
private $id;
private $name;
private $health;
private $power;
public function __construct($pId, $pName, $pHealth, $pPower)
{
$this->id = $pId;
$this->name = $pName;
$this->health = $pHealth;
$this->power = $pPower;
}
public function getName(){
return $this->name;
}
public function getHealth(){
return $this->health;
}
public function getPower(){
return $this->power;
}
}
class HeroFormatter{
public static function toHtml(Hero $pHero){
$str = "<p>";
$str .= "<span>#Refactored# </span><br/>";
$str .= "<span>Name: " . $pHero->getName() . " </span><br/>";
$str .= "<span>Health: " .$pHero->getHealth() . " </span><br/>";
$str .= "<span>Power: " .$pHero->getPower() . " </span><br/>";
$str .= "</p>";
return $str;
}
}
$hulk = new Hero(1, "Hulk", 100, 200);
print HeroFormatter::toHtml($hulk);