1 | <?php |
||
23 | class SettingsCollectionAbstractFactory implements AbstractFactoryInterface |
||
24 | { |
||
25 | /** |
||
26 | * Can the factory create an instance for the service? |
||
27 | * |
||
28 | * @param ContainerInterface $container |
||
29 | * @param string $requestedName |
||
30 | * @return bool |
||
31 | */ |
||
32 | 2 | public function canCreate(ContainerInterface $container, $requestedName) |
|
42 | |||
43 | /** |
||
44 | * Create an object |
||
45 | * |
||
46 | * @param ContainerInterface $container |
||
47 | * @param string $requestedName |
||
48 | * @param null|array $options |
||
49 | * @return object |
||
50 | * @throws ServiceNotFoundException if unable to resolve the service. |
||
51 | * @throws ServiceNotCreatedException if an exception is raised when |
||
52 | * creating a service. |
||
53 | * @throws ContainerException if any other error occurs |
||
54 | */ |
||
55 | 1 | public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
|
70 | |||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | * |
||
74 | * @deprecated will be removed after zf2 support drop |
||
75 | */ |
||
76 | public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | * |
||
84 | * @deprecated will be removed after zf2 support drop |
||
85 | */ |
||
86 | public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
||
90 | } |
||
91 |
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: