1 | <?php |
||
9 | class TwitterCoordinatesSerializer implements TwitterSerializer |
||
10 | { |
||
11 | /** |
||
12 | * @param TwitterSerializable $object |
||
13 | * @return \stdClass |
||
14 | */ |
||
15 | 6 | public function serialize(TwitterSerializable $object) |
|
27 | |||
28 | /** |
||
29 | * @param \stdClass $obj |
||
30 | * @param array $context |
||
31 | * @return TwitterCoordinates |
||
32 | */ |
||
33 | 3 | public function unserialize($obj, array $context = []) |
|
43 | 15 | ||
44 | /** |
||
45 | 15 | * @param TwitterSerializable $object |
|
46 | * @return boolean |
||
47 | */ |
||
48 | public function canSerialize(TwitterSerializable $object) |
||
52 | |||
53 | /** |
||
54 | * @param \stdClass $object |
||
55 | * @return boolean |
||
56 | */ |
||
57 | public function canUnserialize($object) |
||
61 | |||
62 | /** |
||
63 | * @return TwitterCoordinatesSerializer |
||
64 | */ |
||
65 | public static function build() |
||
69 | } |
||
70 |
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 implementation 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 interface: