1 | <?php |
||
16 | class CreateChargeRequest implements RequestInterface |
||
17 | { |
||
18 | /** |
||
19 | * Default endpoint (Uri in Guzzle context) |
||
20 | */ |
||
21 | const ENDPOINT = 'charges/v1'; |
||
22 | /** |
||
23 | * @var Charge |
||
24 | */ |
||
25 | private $charge; |
||
26 | |||
27 | /** |
||
28 | * CreateChargeRequest constructor. |
||
29 | */ |
||
30 | 3 | public function __construct(Charge $charge) |
|
34 | |||
35 | |||
36 | /** |
||
37 | * Return the uri |
||
38 | * |
||
39 | * @return string The uri |
||
40 | */ |
||
41 | 1 | public function getEndpoint() |
|
45 | |||
46 | /** |
||
47 | * Return request method |
||
48 | * |
||
49 | * @return string This is the request type |
||
50 | */ |
||
51 | 1 | public function getRequestMethod() |
|
55 | |||
56 | |||
57 | /** |
||
58 | * Retrieve the whole request |
||
59 | * |
||
60 | * @return string[] The complete request |
||
61 | */ |
||
62 | 1 | public function getPayload() |
|
66 | } |
||
67 |
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.