1 | <?php |
||
16 | class UserService extends AbstractHyperwalletService |
||
17 | { |
||
18 | /** |
||
19 | * @return ListResponse |
||
20 | * |
||
21 | * @throws HyperwalletApiException |
||
22 | */ |
||
23 | public function list(): ListResponse |
||
29 | |||
30 | /** |
||
31 | * @param array $userParameters |
||
32 | * @return User | Exception |
||
33 | */ |
||
34 | public function create(array $userParameters) |
||
43 | |||
44 | /** |
||
45 | * @param array $userParameters |
||
46 | * @return User | Exception |
||
47 | */ |
||
48 | public function update(array $userParameters) |
||
65 | |||
66 | /** |
||
67 | * @param string $userToken |
||
68 | * @return Exception|User |
||
69 | */ |
||
70 | public function get(string $userToken) |
||
78 | |||
79 | /** |
||
80 | * @param string $userToken |
||
81 | * @return Exception|ListResponse |
||
82 | */ |
||
83 | public function listTransferMethods(string $userToken) |
||
91 | |||
92 | /** |
||
93 | * @param string $userToken |
||
94 | * @return Exception|ListResponse |
||
95 | */ |
||
96 | public function listBalances(string $userToken) |
||
104 | |||
105 | /** |
||
106 | * @param string $userToken |
||
107 | * @return Exception|AuthenticationToken |
||
108 | */ |
||
109 | public function getAuthenticationToken(string $userToken) |
||
117 | } |
||
118 |
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.