chantown Posted August 28, 2007 Share Posted August 28, 2007 What is the difference? When would you use one or the other? I mean, they both provide a format for child classes to extend from. Quote Link to comment https://forums.phpfreaks.com/topic/66979-php-abstract-vs-interface/ Share on other sites More sharing options...
hostfreak Posted August 28, 2007 Share Posted August 28, 2007 The only difference is that with abstract you can define methods contents as where with an interface you can only declare the method's signature. Quote Link to comment https://forums.phpfreaks.com/topic/66979-php-abstract-vs-interface/#findComment-335908 Share on other sites More sharing options...
chantown Posted August 28, 2007 Author Share Posted August 28, 2007 so..whats the point of using "interface"? Why not just use abstract all the time Quote Link to comment https://forums.phpfreaks.com/topic/66979-php-abstract-vs-interface/#findComment-335938 Share on other sites More sharing options...
vitcec Posted August 28, 2007 Share Posted August 28, 2007 From what i understand, you use an abstract class when you want to create subclasses which share a common set of functions AND variables. For instance you have an abstract class Animal with variables which help define ANY animal, such as hunger, happiness, etc.. When you extend it with a class such as Dog, the Dog has all the variables and functions any Animal would have; it would still have happiness and hunger. However, you then can declare your own variables which only a Dog would have such as breed. On the other hand with an interface you only define a set of functions that will be included in a class. Quote Link to comment https://forums.phpfreaks.com/topic/66979-php-abstract-vs-interface/#findComment-335954 Share on other sites More sharing options...
448191 Posted August 28, 2007 Share Posted August 28, 2007 An interface defines an interface. An abstract class defines an interface, but can also define common behaviour for it's decendants. Inheritance (using 'extends') is a 'generalization', implementing an interface is an 'realization'. This only emphasizes the fact that interfaces do not define behaviour, only suggests a certain quality. The difference is sometimes marginal, and indeed you can often use an abstract class instead of an interface. However, a class can implement a multitude of interfaces, which can be used as a signal to client code that the object can be used in certain way. For example, an object may implement the interface Serializable, which is a signal to client code that the object can be used with serialization. Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/66979-php-abstract-vs-interface/#findComment-336046 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.