Created
May 14, 2013 19:50
-
-
Save toin0u/5578940 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface ProviderInterface | |
{ | |
public function foo(); | |
} | |
abstract class AbstractProvider implements ProviderInterface | |
{ | |
public function bar() | |
{ | |
return "bar\n"; | |
} | |
// foo() is not implemented here | |
} | |
class MyProvider extends AbstractProvider | |
{ | |
public function foo() | |
{ | |
return "foo\n"; | |
} | |
} | |
$provider = new MyProvider; | |
echo $provider->foo(); // foo | |
echo $provider->bar(); // bar |
I totaly agree :)
I do prefer having the interface defined in the concrete implementation
This implementation gives flexibility :)
If I want to drop the abstract class for some reasons, I can
Thanks for this explanation.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Naa, it's an abstract class, not an abstract implementation of a provider. If I needed an abstract implementation of a provider, that abstract class would implement the
ProviderInterface
, and this abstract class would implement the methods (partially or not) of this interface. Is it the case? No, because of that.So no need to add an interface on such an abstract class. As it is abstract, I can't instantiate it, so naming is correct here. I do prefer having the interface defined in the concrete implementation, than somewhere hidden in an abstract class. If the class was named
BaseProvider
, thing would be different.If I want to drop the abstract class for some reasons, I can, and all concrete providers will still fit the interface. What is important is that concrete providers implement the interface, that is the contract.