DEV Community

Cover image for Number one question on every PHP interview
Damian Brdej
Damian Brdej

Posted on

4

Number one question on every PHP interview

I am a PHP programmer and have been on many job interviews. I noticed that on each of them one question always came up.

That question is:

What's the difference between abstract class and interface?

The answer to this question is simple and proves the candidate's familiarity with object-oriented programming.

So let's compare these two:

Abstract class

  • It can provide some functionality and leave the rest for the derived class.
  • The derived class may or may not override the concrete functions defined in the base class.
  • A child class extended from an abstract class should logically be related.

To declare class to be abstract just simply abstract before class keyword

Interface

  • It cannot contain any functionality. It only contains definitions of the methods.
  • The derived class MUST provide code for all the methods defined in the interface.
  • Completely different and non-related classes can be logically grouped together using an interface.

To declare interface use interface keyword

interface Template
{
    public function setVariable($name, $var);
    public function getHtml($template);
}
Enter fullscreen mode Exit fullscreen mode

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay