1 | <?php |
||
9 | class ZfcUserLoginWidget extends AbstractHelper |
||
10 | { |
||
11 | /** |
||
12 | * Login Form |
||
13 | * @var LoginForm |
||
14 | */ |
||
15 | protected $loginForm; |
||
16 | |||
17 | /** |
||
18 | * $var string template used for view |
||
19 | */ |
||
20 | protected $viewTemplate; |
||
21 | /** |
||
22 | * __invoke |
||
23 | * |
||
24 | * @access public |
||
25 | * @param array $options array of options |
||
26 | * @return string |
||
27 | */ |
||
28 | public function __invoke($options = array()) |
||
29 | { |
||
30 | $options += array( |
||
31 | 'render' => true, |
||
32 | 'redirect' => false, |
||
33 | 'enableRegistration' => false, |
||
34 | ); |
||
35 | |||
36 | $vm = new ViewModel(array( |
||
37 | 'loginForm' => $this->getLoginForm(), |
||
38 | 'redirect' => $options['redirect'], |
||
39 | 'enableRegistration' => $options['enableRegistration'], |
||
40 | )); |
||
41 | $vm->setTemplate($this->viewTemplate); |
||
42 | if ($options['render']) { |
||
43 | return $this->getView()->render($vm); |
||
44 | } |
||
45 | |||
46 | return $vm; |
||
|
|||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Retrieve Login Form Object |
||
51 | * @return LoginForm |
||
52 | */ |
||
53 | public function getLoginForm() |
||
57 | |||
58 | /** |
||
59 | * Inject Login Form Object |
||
60 | * @param LoginForm $loginForm |
||
61 | * @return ZfcUserLoginWidget |
||
62 | */ |
||
63 | public function setLoginForm(LoginForm $loginForm) |
||
68 | |||
69 | /** |
||
70 | * @param string $viewTemplate |
||
71 | * @return ZfcUserLoginWidget |
||
72 | */ |
||
73 | public function setViewTemplate($viewTemplate) |
||
78 | } |
||
79 |
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.