Is a php interface redundant for apps build by a single developer?
My question is : in PHP are interfaces really useful for developers that build website applications on their own ? Isn't an abstract class providing all the things an interface provides ?
If an interface is just a "contract" isn't the developer aware of the things the class should implement ?
The only single benefit I can thin开发者_如何转开发k of is that a class can implement multiple interfaces but again how useful is this .. when you know everything a class should implement. You just force yourself to implement those methods.
As you can figure out I'm still waiting for that A-HA moment when I really understand why an interface is useful.
To wrap this up and put it simple : When should I use interfaces and why not use abstract classes instead ?
Just because you "know" what something should implement, doesn't mean you remember it. It doesn't mean you'll never make a mistake and typo a function name. "Contracts" in programming aren't just for one developer to enforce things on another - they also let you provide a rigidity to the code that can catch mistakes that might otherwise slip under the radar.
You can use interfaces for
- Type-hinting in functions
public function foo(IWhatever $x)
- To check for type
$x instanceof IWhatever
- To create mock objects in unit tests
$this->getMock('IWhatever')
Of course, you could use abstract classes too, but if you don't actually need to define any code it's probably a better idea to use an interface.
"Programming to an Interface and not an implementation" is a principle introduced by the GoF in their books Design Patterns: Elements of Reusable Object-Oriented Software.
Quoting Erich Gamma on the principe:
Once you depend on interfaces only, you're decoupled from the implementation. That means the implementation can vary, and that's a healthy dependency relationship. For example, for testing purposes you can replace a heavy database implementation with a lighter-weight mock implementation. […]
So this approach gives you flexibility, but it also separates the really valuable part, the design, from the implementation, which allows clients to be decoupled from the implementation. One question is whether you should always use a Java interfaces for that. An abstract class is good as well. In fact, an abstract class gives you more flexibility when it comes to evolution. You can add new behavior without breaking clients. […]
In Java when you add a new method to an interface, you break all your clients. When you have an abstract class, you can add a new method and provide a default implementation in it. All the clients will continue to work. As always there is a trade-off, an interface gives you freedom with regard to the base class, an abstract class gives you the freedom to add new methods later. It isn't always possible to define an interface in an abstract class, but in the light of evolution you should consider whether an abstract class is sufficient.
Read the full interview here
So, You can use an interface or an abstract class. You just have to consider the trade-off. IMO, it's worth using interfaces, even if you are alone. You rarely know what your app will look in the end. The waterfall is a myth, so you will have to face change during development and interfaces make it easier to embrace it.
You might also be interested in:
- What is the point of interfaces in a weakly-typed language like PHP?
- Program to an interface not an implementation in php
- why interfaces in dynamic/loosely-typed languages?
- What is the point of interfaces in PHP?
- Why is it possible to have an interface without a return type in PHP?
and some more:
- https://stackoverflow.com/search?q=weakly+typed+interfaces+php
Interface is very good practice in command development. It creates a integrity of program product. First time team writes interface, after abstract classes (with common methods and abstract methods).
Abstract class useful where we extends it in different classes. For example, method __construct() common for all child classes, but others methods are different.
Our team uses this model: Interface->Abstract->Class1->Class2
Class1 extends Abstract implements Interface
Class2 extends Abstract implements Interface
Some time since this was asked but as the functionality of interfaces seems to puzzle many people - if you come here looking, try the example on here with 500+ votes. I found it really helpful.
What does it mean to "program to an interface"?
When should I use interfaces and why not use abstract classes instead ?
As soon as you use e.g the factory pattern an interface should be used. Im sure there are more examples for it.
Take a look at the various design patterns. http://www.ibm.com/developerworks/library/os-php-designptrns/
EDIT: changed link to a better explanation.
精彩评论