glenelkins Posted June 5, 2009 Share Posted June 5, 2009 Hi I would like some input on peoples oppinions on the use of interfaces over abstract classes. take the following example: Abstract class Test { Abstract function myTest ( $testVar ); } class Test2 extends Test { function myTest ( $testVar ) { echo $testVar; } } Now this: interface Test { public function myTest ( $testVar ); } class Test2 implements Test { function myTest ( $testVar ) { echo $testVar; } } whats the difference? Link to comment https://forums.phpfreaks.com/topic/161060-lets-compare-interface-to-abstract-class/ Share on other sites More sharing options...
Ken2k7 Posted June 5, 2009 Share Posted June 5, 2009 Abstract changed to interface and extends changed to implements. Though, those aren't the best test cases for them. In an abstract class, you can define the method myTest whereas you can't in an interface. Link to comment https://forums.phpfreaks.com/topic/161060-lets-compare-interface-to-abstract-class/#findComment-849962 Share on other sites More sharing options...
anupamsaha Posted June 5, 2009 Share Posted June 5, 2009 Interfaces are a common set of methods those can be implemented across various types of classes dealing with various objectives. In case of an abstract class, its something like a factory containing set of a process. Does it make sense? Link to comment https://forums.phpfreaks.com/topic/161060-lets-compare-interface-to-abstract-class/#findComment-849964 Share on other sites More sharing options...
glenelkins Posted June 5, 2009 Author Share Posted June 5, 2009 Ken2k7 : Why cant i define myTest in an interface? The idea is to give a common ground of methods, defenitions etc anupamsaha: but again, they perform the same! - in fact i just realised what you have said. THe abstract class yeh is like a factory that demands that certain functions are specified. The interface just defines functions that can be used and if they are used they have to have the specified params in the interface..correct? Link to comment https://forums.phpfreaks.com/topic/161060-lets-compare-interface-to-abstract-class/#findComment-849968 Share on other sites More sharing options...
glenelkins Posted June 5, 2009 Author Share Posted June 5, 2009 this is a good quote: Interface is a class with no data members and contains only member functions and they lack its implementation. Any class that inherits from an interface must implement the missing member function body. Interfaces is also an abstract class because abstract class always require an implementation. In PHP 5 class may inherit only one class, but because interfaces lack an implementation any number of class can be inherited. In PHP 5, interfaces may declare only methods. An interface cannot declare any variables so they are very similar! Link to comment https://forums.phpfreaks.com/topic/161060-lets-compare-interface-to-abstract-class/#findComment-850134 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.