1 | <?php |
||
19 | class InterfaceManager implements RenderableInterface, DumpableInterface, StructureWithMethodsInterface |
||
20 | { |
||
21 | use TemplateTrait; |
||
22 | |||
23 | /** |
||
24 | * @Assert\NotNull(message = "Interface has to know about class!") |
||
25 | * @Assert\Valid() |
||
26 | */ |
||
27 | private $classManager = null; |
||
28 | |||
29 | /** |
||
30 | * @var ArrayCollection |
||
31 | * @Assert\NotNull(message = "Interface methods collection can not be emtpy") |
||
32 | * @Assert\Valid() |
||
33 | */ |
||
34 | private $methods = null; |
||
35 | |||
36 | /** |
||
37 | * Construct |
||
38 | * |
||
39 | * @param ClassManager $classManager |
||
40 | */ |
||
41 | public function __construct(ClassManager $classManager) |
||
46 | |||
47 | /** |
||
48 | * @Assert\IsTrue(message = "Invalid interface namespace, check yaml schema! eg. \AppBundle\Location\Entity") |
||
49 | * @return boolean |
||
50 | */ |
||
51 | public function isValidNamespace() |
||
55 | |||
56 | /** |
||
57 | * @return ClassManager |
||
58 | */ |
||
59 | public function getClassManager() |
||
63 | |||
64 | /** |
||
65 | * @param ClassManager $classManager |
||
66 | * @return InterfaceManager |
||
67 | */ |
||
68 | public function setClassManager(ClassManager $classManager) |
||
73 | |||
74 | /** |
||
75 | * Return namespace |
||
76 | * |
||
77 | * @return string |
||
78 | */ |
||
79 | public function getNamespace() |
||
83 | |||
84 | /** |
||
85 | * Return collection of methods |
||
86 | * |
||
87 | * @return ArrayCollection |
||
88 | */ |
||
89 | public function getMethods() |
||
93 | |||
94 | /** |
||
95 | * Set collection of methods |
||
96 | * |
||
97 | * @param ArrayCollection $methods |
||
98 | * @return InterfaceManager |
||
99 | */ |
||
100 | public function setMethods(ArrayCollection $methods) |
||
105 | |||
106 | /** |
||
107 | * Return comment for interface |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | public function getComment() |
||
115 | |||
116 | /** |
||
117 | * Return name of class/interface from namespace |
||
118 | * |
||
119 | * @return string |
||
120 | * @throws Exception |
||
121 | */ |
||
122 | public function getName() |
||
126 | |||
127 | /** |
||
128 | * Return namespace without name - for rendering namespace in class |
||
129 | * |
||
130 | * @return string |
||
131 | * @throws Exception |
||
132 | */ |
||
133 | public function getNamespaceWithoutName() |
||
137 | |||
138 | /** |
||
139 | * Return namespace without name - for createing directory |
||
140 | * |
||
141 | * @return string |
||
142 | * @throws Exception |
||
143 | */ |
||
144 | public function getDirectory() |
||
148 | |||
149 | /** |
||
150 | * Return namespace without name - for rendering namespace in class |
||
151 | * |
||
152 | * @return string |
||
153 | * @throws Exception |
||
154 | */ |
||
155 | public function getNamespaceWithoutNameAndBackslashPrefix() |
||
159 | |||
160 | /** |
||
161 | * Return set of tags used in template |
||
162 | * |
||
163 | * @return array |
||
164 | */ |
||
165 | public function getTemplateTags() |
||
174 | } |
||
175 |
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.