1 | <?php |
||
25 | class Reflection extends ProxyDictionary implements ReflectionInterface |
||
26 | { |
||
27 | /** |
||
28 | * @var int |
||
29 | */ |
||
30 | public const LOAD_STDLIB = 0x02; |
||
31 | |||
32 | /** |
||
33 | * @var int |
||
34 | */ |
||
35 | public const LOAD_INTROSPECTION = 0x04; |
||
36 | |||
37 | /** |
||
38 | * @var array|DocumentInterface[] |
||
39 | */ |
||
40 | protected $documents = []; |
||
41 | |||
42 | /** |
||
43 | * Reflection constructor. |
||
44 | * @param Dictionary|null $parent |
||
45 | * @param int $options |
||
46 | * @throws Exception\TypeConflictException |
||
47 | * @throws \Railt\Io\Exception\ExternalFileException |
||
48 | */ |
||
49 | 9 | public function __construct(Dictionary $parent = null, int $options = self::LOAD_INTROSPECTION | self::LOAD_STDLIB) |
|
55 | |||
56 | /** |
||
57 | * @param int $options |
||
58 | * @return void |
||
59 | * @throws Exception\TypeConflictException |
||
60 | * @throws \Railt\Io\Exception\ExternalFileException |
||
61 | */ |
||
62 | 9 | private function boot(int $options): void |
|
72 | |||
73 | /** |
||
74 | * @param DocumentInterface $document |
||
75 | */ |
||
76 | 17 | public function addDocument(DocumentInterface $document): void |
|
80 | |||
81 | /** |
||
82 | * @return iterable |
||
83 | */ |
||
84 | 4 | public function getDocuments(): iterable |
|
88 | |||
89 | /** |
||
90 | * @param TypeDefinition $type |
||
91 | * @return Dictionary |
||
92 | */ |
||
93 | 17 | public function add(TypeDefinition $type): Dictionary |
|
99 | } |
||
100 |
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.