Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a ABSTRACT CLASS and INTERFACE and when would you use them in PHP?

What is a abstract class and interface in PHP and when would you use them? A layman answer with an example would be great to aid my understanding.

like image 801
Imran Avatar asked Sep 15 '10 22:09

Imran


People also ask

WHAT IS interface and abstract class when to use what?

Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing a common functionality to unrelated classes. Interfaces are a good choice when we think that the API will not change for a while.

Why would you use an abstract class Why would you use an interface?

The short answer: An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it. And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces.

What is use of abstract class in PHP?

Abstract classes and methods are when the parent class has a named method, but need its child class(es) to fill out the tasks. An abstract class is a class that contains at least one abstract method. An abstract method is a method that is declared, but not implemented in the code.

WHAT IS interface and why it is used PHP?

A PHP interface defines a contract which a class must fulfill. If a PHP class is a blueprint for objects, an interface is a blueprint for classes. Any class implementing a given interface can be expected to have the same behavior in terms of what can be called, how it can be called, and what will be returned.


4 Answers

Abstract classes and interfaces are not specific to PHP; they're features of many modern object-oriented languages.

An abstract class is a class that has one or more unimplemented member functions. You would use one in a situation where you would want a set of classes that have similar behaviors, differing only in a few methods. Each of these classes would derive from the abstract class, and implement the unimplemented methods in the manner that's appropriate for their specific case.

An interface is similar to an abstract class, but it contains no method implementations, only signatures. A class that implements an interface must implement all of the methods.

A class can implement many interfaces, but can only be derived from one abstract class (or parent class of any sort), since PHP does not support multiple inheritance.

Both abstract classes and interfaces allow for polymorphism; i.e., you can specify a reference as being to an abstract type (which could refer to any instance of class derived from it) or to an object implementing an interface.

like image 95
ngroot Avatar answered Oct 24 '22 17:10

ngroot


Assuming you understand why you use them in other languages, the same concepts apply to PHP. Interfaces are class "signatures"... they contain no code. Abstract classes cannot be instantiated, but they can contain both implemented and abstract (empty) functions.

Note that a class can only extend a single parent class (or abstract class), but it can implement as many interfaces as it needs.

But given that PHP does not have strict typing and has no concept of compile time checks, you can typically make do without them, unlike in languages like C++ or Java.

The core PHP require interfaces for some things. For example:

class Foo implements Countable
{
  public function count()
  {
    return 5;
  }
}

echo count(new Foo()); // returns 5

If you remove the "implements Countable," PHP will not call your count method. There are many other interfaces you can implement that allows custom objects to all behave in a similar way. For instance, you can create an object that works with a foreach iteration.

Interfaces at the user level aren't extremely useful in PHP, as I previously mentioned. But if you make use of type hints, they can be:

function foo(MyInterface $bar)
{
  $bar->someMethod();
}

Assuming someMethod is declared in MyInterface, then you are guaranteed that the call to $bar->someMethod() exists. (The reason why this isn't as useful in PHP as it is in other languages, is because the type hint in the function signature won't get checked until runtime.)

Empty interfaces can be useful too as ways to categorize classes. Say you want to do something to a particular set of objects. They could all implement some empty interface. Then your code could just check for that interface without ever having to know anything about the object.

interface Red {}

class Foo implements Red { }

$foo = new Foo();
if ($foo instanceof Red)
{
  // do something
}

Abstract classes are useful when you have a lot of code that is shared among sub classes, but isn't meaningful by itself. You can declare the base class as abstract, implement all the shared functionality, and leave the rest of the functions empty. By declaring that base class as abstract, nobody will ever be able to use it by itself.

like image 43
Matthew Avatar answered Oct 24 '22 18:10

Matthew


The PHP manual explains it simply and provides many examples:

  • Interfaces
  • Abstraction
like image 24
Kevin Avatar answered Oct 24 '22 19:10

Kevin


Understanding and Applying Polymorphism in PHP

like image 33
coolgeek Avatar answered Oct 24 '22 19:10

coolgeek