1 | <?php |
||
13 | class Action implements ActionInterface |
||
14 | { |
||
15 | /** |
||
16 | * Endpoint for the action |
||
17 | * |
||
18 | * @const ENDPOINT |
||
19 | */ |
||
20 | const ENDPOINT = 'https://api.digitalocean.com/v2/droplets/[:id:]/actions'; |
||
21 | |||
22 | /** |
||
23 | * HTTP Method |
||
24 | * |
||
25 | * @const METHOD |
||
26 | */ |
||
27 | const METHOD = 'POST'; |
||
28 | |||
29 | /** |
||
30 | * ID of the droplet to perform the action on |
||
31 | * |
||
32 | * @var mixed $id |
||
33 | */ |
||
34 | protected $id; |
||
35 | |||
36 | /** |
||
37 | * Set the droplet id |
||
38 | * |
||
39 | * @param mixed $id |
||
40 | */ |
||
41 | public function setId($id) |
||
45 | |||
46 | /** |
||
47 | * Method to return the action request object |
||
48 | * |
||
49 | * @param Array $headers |
||
50 | * @param string $body |
||
51 | * @return \GuzzleHttp\Message\Request |
||
52 | */ |
||
53 | public function getRequest(Array $headers = [], $body = '') |
||
76 | } |
||
77 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: