1 | <?php |
||
7 | class IP implements Host |
||
8 | { |
||
9 | /** |
||
10 | * @var string |
||
11 | */ |
||
12 | private $value; |
||
13 | |||
14 | /** |
||
15 | * Initialize IP. |
||
16 | * |
||
17 | * @param $value |
||
18 | * |
||
19 | * @throws InvalidArgumentException |
||
20 | */ |
||
21 | private function __construct($value) |
||
29 | |||
30 | /** |
||
31 | * Create IP from string. |
||
32 | * |
||
33 | * @param $string |
||
34 | * |
||
35 | * @return IP |
||
36 | */ |
||
37 | public static function fromString($string) |
||
41 | |||
42 | /** |
||
43 | * Create IP from current request. |
||
44 | * |
||
45 | * @return IP |
||
46 | */ |
||
47 | public static function fromCurrentRequest() |
||
51 | |||
52 | /** |
||
53 | * Compare equality with another Host. |
||
54 | * |
||
55 | * @param Host $host |
||
56 | * |
||
57 | * @return bool |
||
58 | */ |
||
59 | public function equals(Host $host) |
||
63 | |||
64 | /** |
||
65 | * Get string representation of IP. |
||
66 | * |
||
67 | * @return string |
||
68 | */ |
||
69 | public function toString() |
||
73 | |||
74 | /** |
||
75 | * Cast IP to string. |
||
76 | * |
||
77 | * @return string |
||
78 | */ |
||
79 | public function __toString() |
||
83 | } |
||
84 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: