1 | <?php |
||
10 | class Text extends DataTypeAbstract implements DataTypeInterface |
||
11 | { |
||
12 | const DATA_TYPE_ERROR = 'Assertion failed for value "%s" for "%s" : INVALID_TYPE'; |
||
13 | const DATA_LENGTH = 'Assertion failed for value "%s" for "%s" : INVALID_TEXT_LENGTH'; |
||
14 | |||
15 | protected static $defaults = [ |
||
16 | 'default' => '', |
||
17 | 'min' => 0, |
||
18 | 'max' => null, |
||
19 | 'pad' => 'left', |
||
20 | 'left_pad' => ' ', |
||
21 | 'right_pad' => ' ' |
||
22 | ]; |
||
23 | |||
24 | protected static $padOptions = [ |
||
25 | 'left' => STR_PAD_LEFT, |
||
26 | 'right' => STR_PAD_RIGHT |
||
27 | ]; |
||
28 | |||
29 | /** |
||
30 | * Boolean constructor. |
||
31 | * @param string $key |
||
32 | * @param mixed $datum |
||
33 | * @param array $options |
||
34 | * @throws validArgumentException |
||
35 | */ |
||
36 | public function __construct(string $key, $datum, array $options = []) |
||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | public function assert() |
||
52 | |||
53 | private function isString() |
||
63 | |||
64 | private function checkLength() |
||
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | public function normalize() |
||
107 | } |
||
108 |
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_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.