1 | <?php |
||
7 | class UserWithPassword implements User, PasswordReset |
||
8 | { |
||
9 | /** |
||
10 | * @var string |
||
11 | */ |
||
12 | protected $username; |
||
13 | |||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $password; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $displayName; |
||
23 | |||
24 | /** |
||
25 | * @var PasswordResetToken |
||
26 | */ |
||
27 | protected $passwordResetToken; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $email; |
||
33 | |||
34 | /** |
||
35 | * @var int |
||
36 | */ |
||
37 | protected $id; |
||
38 | |||
39 | /** |
||
40 | * @param string $username |
||
41 | * @param string $password |
||
42 | * @param string $displayName |
||
43 | * @param string $email |
||
44 | * @param int $id |
||
45 | * @param PasswordResetToken $token |
||
46 | */ |
||
47 | public function __construct( |
||
65 | |||
66 | /** |
||
67 | * {@inheritDoc} |
||
68 | */ |
||
69 | public function getRoles() |
||
73 | |||
74 | /** |
||
75 | * {@inheritDoc} |
||
76 | */ |
||
77 | public function getPassword() |
||
81 | |||
82 | /** |
||
83 | * {@inheritDoc} |
||
84 | */ |
||
85 | public function getSalt() |
||
89 | |||
90 | /** |
||
91 | * {@inheritDoc} |
||
92 | */ |
||
93 | public function getUsername() |
||
97 | |||
98 | /** |
||
99 | * {@inheritDoc} |
||
100 | */ |
||
101 | public function eraseCredentials() |
||
105 | |||
106 | /** |
||
107 | * {@inheritDoc} |
||
108 | */ |
||
109 | public function getDisplayName() |
||
113 | |||
114 | /** |
||
115 | * {@inheritDoc} |
||
116 | */ |
||
117 | public function __toString() |
||
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | public function clearPasswordResetToken() |
||
131 | |||
132 | /** |
||
133 | * @return self |
||
134 | */ |
||
135 | public function generatePasswordResetToken() |
||
141 | |||
142 | /** |
||
143 | * @return string |
||
144 | */ |
||
145 | public function getPasswordResetToken() |
||
149 | |||
150 | /** |
||
151 | * @return string |
||
152 | */ |
||
153 | public function getEmail() |
||
157 | |||
158 | /** |
||
159 | * @param string $password |
||
160 | * |
||
161 | * @return self |
||
162 | */ |
||
163 | public function setPassword($password) |
||
169 | |||
170 | /** |
||
171 | * @return int |
||
172 | */ |
||
173 | public function getId() |
||
177 | } |
||
178 |
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.