PHP Abstract and Interface, a funny and easy-to-understand

·

·

,

Imagine you’re the manager of a Circus. Now, within this circus, you have different performers like Jugglers, Clowns, Fire-breathers, etc. As the manager, you don’t necessarily care how each performer carries out their act (how a clown tells his jokes or how a fire-breather breathes fire). What you do care about is that each performer has an act to perform.

This is where our first concept comes in: Abstract Classes. Picture this as the general “Performers” class in your circus. It might have a method ‘performAct()‘ that every performer class (like Jugglers, Clowns) MUST do, but the way they do it depends on the type of performer. This ‘performAct()‘ method would be an abstract method, with no implementation in the parent abstract class, but a strict order that child classes must ‘performAct()‘ in their individual way.

Here’s a simple PHP representation:

abstract class Performer {
    abstract public function performAct();
}

class Clown extends Performer {
    public function performAct() {
        // Clown's way of performing act
    }
}

class Juggler extends Performer {
    public function performAct() {
        // Juggler's way of performing act
    }
}

Now, onto the second concept: Interfaces. Imagine now that we have some performers who ride unicycles. It doesn’t matter if they are clowns, jugglers, or even a fire-breather. If they’re unicycle riders, they all need to know how to ‘rideUnicycle()‘. It’s an extra skill beyond their regular act, not all performers will have it. ‘rideUnicycle()‘ is not necessarily related to their main act but an additional thing they do.

That would be an interface, ‘UnicycleRiders‘! You’re not telling them how to ride it, you’re just saying, “If you’re a unicycle rider, you better know how to do this”.

And here’s the PHP code:

interface UnicycleRiders {
   public function rideUnicycle();
}

class Clown extends Performer implements UnicycleRiders {
    public function performAct() {
        // Clown's way of performing act
    }

    public function rideUnicycle() {
        // Clown's way of riding unicycle
    }
}

When to use which?

Abstract classes are typically used when similar classes have the same behavior (methods) and some shared data (properties). It’s said they have a rich relationship.

Interfaces, on the other hand, are used when we want to specify a method that classes should implement, but they’re unrelated. It’s like a label saying, “I promise I do have this functionality”. They have a “need-to-know” relationship.

The big lesson: Abstract classes dictate “who you are” and Interfaces dictate “what you can do“.


Meet the author

Faisal Ahammad

I’m working as Support Engineer at Saturday Drive Inc (AKA Ninja Forms) and General Translation Editor (GTE) for the #bn_BD 🇧🇩 language. As an active contributor to WordPress and open-source projects, I have translated over 60 themes, plugins, and WordPress core. I also have a small YouTube channel where I share my knowledge.


Leave a Reply

Your email address will not be published. Required fields are marked *