Skip to content

Instantly share code, notes, and snippets.

@toin0u
Created May 14, 2013 19:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toin0u/5578940 to your computer and use it in GitHub Desktop.
Save toin0u/5578940 to your computer and use it in GitHub Desktop.
<?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
@willdurand
Copy link

yeah, but I don't like the idea of using an interface on an abstract class. the abstract class is not implementing the interface, it is just there to provide useful methods, and avoid duplicate code.

@makasim
Copy link

makasim commented May 15, 2013

AbstractProvider is not just a set of useful methods, and avoid duplicate code.. It is not completed provider, but still it is provider. It designed to be used as provider. And it is a bad idea to extend this class and use as something else, not provider. So if it is provider it must restrict concrete\child providers to implement provider interface.

If I wanted to have useful methods, and avoid duplicate code. I would create an abstract class but I would never call it provider (word provider in class name is what confusing me).

@toin0u
Copy link
Author

toin0u commented May 15, 2013

I agree with @willdurand. But I understand your point of view...

The current implementation is not wrong at all. But yes, AbstractProvider should perhaps be named AbstractProviderTools or somthing like that. Personally, I do prefer AbstractProvider ;)

Anyway it is a very good question. I don't know what the best practice here.

@willdurand
Copy link

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.

@toin0u
Copy link
Author

toin0u commented May 15, 2013

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