Solutions
Latest Projects
Inovahire
Inovahire
Inovahire
Contact

We Know What Works!

Wide Web Way is the best web development company in the middle east targeting KSA, UAE, Jordan and Arab World in general. It has a wide range of tools and experienced team members to help you achieve your goals, whether you're expanding on an existing website, or just starting out.

Interface VS Abstract Class in PHP

Interface and class Abstract are concepts of OOP in PHP underused. The reasons behind lack of use for those two concepts are:

  • unclear requirements (or unclear gone by the developer) in the design phase
  • shortage in knowledge

What is an interface?
The clearest definition is that an interface is a contract.
Features:

  • All classes that implement an interface must develop all the methods that have been defined
  • The class implementing the interface must use the exact same method signatures as are defined in the interface. Not doing so will result in a fatal error
  • All methods declared in an interface must be public, this is the nature of an interface
  • A class can implement more than one interface
  • An interface can be used by the Type Hinting
Example:
interface logsInterface
{
 public function log ( $message , $priority = null);
}

What is an abstract class?
It's a kind "father" that must be inherited to be used. Classes that inherit differ from them only in the abstract methods and can access the methods of the parent class using the keyword parent . Features:

  • can not be instantiated
  • methods can be abstract (not implemented)
  • methods may be not abstract (implemented)
  • a class can inherit from a single abstract class
Example
abstract class logsAbstract
{
 abstract public function logMethod (); 
 public function getVariable ()
 {
  return $this->variable;
 }
}

SUMMARY OF INTERFACE VS ABSTRACT CLASS

Abstract Class Interface
For abstract class a method must be declared as abstract. Abstract methods doesn’t have any implementation. For interface all the methods by default are abstract methods only. So one cannot declare variables or concrete methods in interfaces.
The Abstract methods can declare with Access modifiers like public, internal, protected. When implementing in subclass these methods must be defined with the same (or a less restricted) visibility. All methods declared in an interface must be public.
Abstract class can contain variables and concrete methods. Interfaces cannot contain variables and concrete methods except constants.
A class can Inherit only one Abstract class and Multiple inheritance is not possible for Abstract class. A class can implement many interfaces and Multiple interface inheritance is possible.
A child class may or may not override a method defined in the parent class A class that implements an interface must override all interface methods
In principle, if an abstract class contains only abstract methods we're using it as an interface.

CONCLUSIONS
Abstract classes are used to share functions.
The interfaces are used to share how you have to do something.

 
 

you are allowed to use <a> , <i>, <b>,<ul>,<li> and <br> tags

Alex Fraundorf said: at 2012-04-10 20:35:34
Very nice article. Thank you for sharing it. I would be interested in hearing an expansion including when you would want to use a concrete method.


Alaa Alomari said: Concrete class at 2012-04-10 20:38:16
@Alex: concrete class is a class that can be directly instantiated. Contrast of abstract class.
what do you like to know about it? however, you can subscribe to our rss feeds :)


Alex Fraundorf said: Concrete class at 2012-04-12 17:52:49
@Alaa: So a concrete class is just any class, with or without a parent, that is not abstract? Is that correct? I was thinking that a concrete class was a special type of class that had to be declared, which is where my confusion was coming from. Thank you.


Alaa Alomari said: Re: Concrete class at 2012-04-14 00:18:33
@Alex: Exactly :)


Alex Fraundorf said: Concrete Class at 2012-04-15 23:10:58
Thank you very much. I really appreciate the clarification. On a side note, the first comment I made was on LinkedIn. It is very cool how it carried over to this page on your blog. I am guessing that you used the LinkedIn API for that.
Again thanks for the clarification and for sharing this post.


Yugant said: PHP at 2012-07-04 05:14:34
Thank you for sharing such a nice article...


 
Blog
Tips and Tricks from the
Web Development Experts