| 1 | <?php declare(strict_types = 1); |
||
| 15 | final class Config implements ConfigContract |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Array of configuration items. |
||
| 19 | * |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | private $items = []; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Construct function. |
||
| 26 | * |
||
| 27 | * @param array $items |
||
| 28 | */ |
||
| 29 | public function __construct(array $items) |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Returns config value for provided key. |
||
| 36 | * |
||
| 37 | * @param string $key |
||
| 38 | * @return mixed |
||
| 39 | */ |
||
| 40 | public function __get(string $key) |
||
| 48 | |||
| 49 | 47 | /** |
|
| 50 | * Checks if config contains value for provided key. |
||
| 51 | * |
||
| 52 | * @param $key |
||
| 53 | * @return bool |
||
| 54 | 19 | */ |
|
| 55 | public function __isset(string $key): bool |
||
| 59 | 19 | ||
| 60 | 13 | /** |
|
| 61 | * @inheritDoc |
||
| 62 | 19 | */ |
|
| 63 | public function count() |
||
| 67 | 19 | ||
| 68 | 19 | /** |
|
| 69 | * @inheritDoc |
||
| 70 | */ |
||
| 71 | public function getIterator() |
||
| 75 | 1 | ||
| 76 | /** |
||
| 77 | * @inheritDoc |
||
| 78 | */ |
||
| 79 | function jsonSerialize() |
||
| 83 | 2 | ||
| 84 | 1 | /** |
|
| 85 | * Returns array representation of config. |
||
| 86 | * |
||
| 87 | * @return array |
||
| 88 | */ |
||
| 89 | 2 | public function toArray(): array |
|
| 93 | } |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.