What is the difference between abstract and interface in php?

Possible duplicate:
PHP: What is the difference between an interface and an abstract class?

Hi guys..

As far as I understand, clase implements or extends abstract or interface classes, should use the default methods. I know that we can use the implementation keyword to use several interfaces, but we can extend only 1 abstract. Can someone explain which one to use in real life and the difference? Thanks to a million !!!!

+4
source share
6 answers

The differences are theoretical and practical:

  • an interface is a description of some of the features that your class has and advertises (therefore, different classes that implement the same interface can be used the same way)
  • An abstract class can be a default implementation containing parts that can appear in all implementations. It should not implement the full interface.

An example is the interface:

// define what any class implementing this must be capable of interface IRetrieveData { // retrieve the resource function fetch($url); // get the result of the retrieval (true on success, false otherwise) function getOperationResult(); // what is this class called? function getMyClassName(); } 

Now we have a set of requirements that will be checked for each class that implements this. Let me make an abstract class and its children:

 // define default behavior for the children of this class abstract class AbstractRetriever implements IRetrieveData { protected $result = false; // define here, so we don't need to define this in every implementation function getResult() { return $result; } // note we're not implementing the other two methods, // as this will be very different for each class. } class CurlRetriever extends AbstractRetriever { function fetch($url) { // (setup, config etc...) $out = curl_execute(); $this->result = !(curl_error()); return $out; } function getMyClassName() { return 'CurlRetriever is my name!'; } } class PhpRetriever extends AbstractRetriever { function fetch($url) { $out = file_get_contents($url); $this->result = ($out !== FALSE); return $out; } function getMyClassName() { return 'PhpRetriever'; } } 

A completely different abstract class (not associated with an interface), with a subclass that implements our interface:

 abstract class AbstractDog { function bark() { return 'Woof!'; } } class GoldenRetriever extends AbstractDog implements IRetrieveData { // this class has a completely different implementation // than AbstractRetriever // so it doesn't make sense to extend AbstractRetriever // however, we need to implement all the methods of the interface private $hasFetched = false; function getResult() { return $this->hasFetched; } function fetch($url) { // (some retrieval code etc...) $this->hasFetched = true; return $response; } function getMyClassName() { return parent::bark(); } } 

Now, in another code, we can do this:

 function getStuff(IRetrieveData $retriever, $url) { $stuff = $retriever->fetch($url); } 

and we don’t need to worry about which retriever (cURL, PHP or Golden) will be passed on and how they will achieve the goal, since everyone should be able to behave in a similar way. You can do this with an abstract class, but then you limit yourself based on the ancestors of the classes, not its capabilities.

+11
source

Somewhat against single inheritance:

  • You can inherit only one abstract class.
  • You can implement several interfaces

Implementation:

  • In an abstract class, valid code may exist. This allows you to share the implementation between child classes.
  • The interface defines only public member functions. Classes that implement the same interface do not actually use code.

This is what I know from my head.

+5
source
 "An Abstract Class can contain default Implementation, where as an Interface should not contain any implementation at all. " 

As for using real-world applications ... it really comes down to context.

For example, the other day there was a question about the implementation of the game using PHP . Here they had an abstract class defining a monster, and any monster can be based on this abstract class. This allowed the default monster properties to be inherited.

While for an interface you define the general requirements for the "interface" method (pardon using the term in the explanation) of a certain system. An example of this is from a recent project that I did. I applied soapclient in php to interact with third party soapserver. This interface determines which soap methods the server supports, and therefore any class that implements my interface must define these methods.

+1
source

Here is a good description of the differences between them:

http://www.supertom.com/code/php_abstracts_and_interfaces.html

It all comes down to the fact that extends is an is-a relationship, while an implementation is an has-a relationship.

+1
source

The metaphor that I heard best was that the abstract class is a half-filled class. It is not done; you still have to finish it. Therefore, when you create a class that extends an abstract class, you simply complete what you started in the abstract class. That is why you cannot instantiate an abstract class; that you made it abstract means that it is incomplete. It still needs extra functionality.

The interface simply ensures that certain methods, each with a certain number of arguments, must exist in the class that implements it. So in the future, a programmer who uses a class that implements a certain interface can be sure that he can call certain methods in this class.

+1
source

All Articles