Jump to content

Lets compare Interface, to Abstract class


glenelkins

Recommended Posts

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?

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?

 

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?

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.