Is php redundant interface for applications created by one developer?

My question is: are PHP very useful for developers who create their own applications themselves? Isn't it an abstract class that provides everything the interface provides?

If an interface is just a โ€œcontractโ€, doesn't the developer know about the things that a class should implement?

The only advantage I can think of is that a class can implement several interfaces, but then again, how useful it is ... when you know everything that a class needs to implement. You just force yourself to implement these methods.

As you can understand, I'm still waiting for this moment of A-HA, when I really understand why the interface is useful.

To wrap this up and make it simple: when should you use interfaces and why not use abstract classes?

+6
oop php interface
source share
6 answers

Just because you โ€œknowโ€ that something needs to be implemented does not mean that you remember it. This does not mean that you will never make a mistake and describe the name of the function. โ€œContractsโ€ in programming are not only designed so that one developer can ensure that other goals are met - they also allow for the rigidity of code that can catch errors that could slip under the radar.

+2
source share

You can use interfaces to

  • Function hint type public function foo(IWhatever $x)
  • To check the type of $x instanceof IWhatever
  • To create a mock of objects in unit tests $this->getMock('IWhatever')

Of course, you can also use abstract classes, but if you really don't need to define any code, it is probably better to use an interface.

+1
source share

"Programming with an interface, not an implementation" is a principle introduced by GoF in his books Design Patterns: Elements of Reusable Object-Oriented Software .

Erich Gamma quote about the principle:

Once you depend only on interfaces, you are separate from the implementation. This means that implementation can vary and that relationship is healthy addiction. For example, for testing purposes, you can replace the implementation of a large database with a lighter breadboard implementation. [...]

Thus, this approach gives you flexibility, but it also separates the really important part, the design, from the implementation, which allows customers to be separate from the implementation. One question is whether Java interfaces should always be used for this. And the abstract class is good. In fact, an abstract class gives you great flexibility when it comes to evolution. You can add new behavior without disassembling clients. [...]

In Java, when you add a new method to an interface, you break up all your clients. When you have an abstract class, you can add a new method and provide it with a default implementation. All customers will continue to work. As always, there is a trade-off, the interface gives you freedom with respect to the base class, the abstract class gives you the freedom to add new methods later. It is not always possible to define an interface in an abstract class, but in 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 need to consider a compromise. IMO, it's worth using interfaces, even if you're alone. You rarely know what your app will look like at the end. A waterfall is a myth , so you have to face changes during development, and the interfaces make it easier to cover.

You may also be interested in:

  • What is the meaning of interfaces in a weakly typed language, such as PHP?
  • Program for interface, not implementation in php
  • why are interfaces in dynamic / weakly typed languages?
  • What is the meaning of interfaces in PHP?
  • Why is it possible to have an interface without return type in PHP?

and a few more:

  • https://stackoverflow.com/search?q=weakly+typed+interfaces+php
+1
source share

The interface is a very good practice in team development. This creates the integrity of the software product. The first time the team writes an interface, after abstract classes (with common methods and abstract methods).

An abstract class is useful where we extend it in different classes. For example, the __construct () method is common to all child classes, but the other methods are different.

Our team uses this model: Interface-> annotations:> Class1-> Class2

 Class1 extends Abstract implements Interface Class2 extends Abstract implements Interface 
0
source share

Some time since it was asked, but since the functionality of the interfaces seems to be a puzzle of many people - if you come here to search, try an example here with more than 500 votes. I found this really helpful.

What does it mean to "program on the interface"?

0
source share

When should I use interfaces and why use abstract classes instead?

As soon as you use, for example, the factory template, you should use the interface. I am sure there are more examples for this.

Take a look at the various design templates. http://www.ibm.com/developerworks/library/os-php-designptrns/

EDIT: Changed the link for a better explanation.

-one
source share

All Articles