1 | <?php |
||
22 | class TwigTemplateTask extends TemplateTask |
||
23 | { |
||
24 | |||
25 | public function name() |
||
29 | |||
30 | /** |
||
31 | * Assembles and writes bakes the twig view file. |
||
32 | * |
||
33 | * @param string $action Action to bake. |
||
34 | * @param string $content Content to write. |
||
35 | * @return string Generated file content. |
||
36 | */ |
||
37 | 4 | public function bake($action, $content = '') |
|
54 | } |
||
55 |
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.