Skip to content

Instantly share code, notes, and snippets.

@djandyr
Last active August 3, 2016 12:55
Show Gist options
  • Save djandyr/646c3c233b1b7b76f97d6a19aeb9523e to your computer and use it in GitHub Desktop.
Save djandyr/646c3c233b1b7b76f97d6a19aeb9523e to your computer and use it in GitHub Desktop.

Interface

Objects define their behavior with the outside world through the methods that they expose. These methods form the object’s interface, when implementing an interface the class formally promises to provide behaviour. If your class implements an interface, all methods defined by that interface must appear in your class before it can be successfully ran.

Imagine your in a empty room, then a zombie attacks - you have no weapon. Luckily a fellow human is standing at the door, you shout "Quick throw me something I can bash this zombie with!". Thinking about this situation you need a weapon which has the following behaviour:

  • Throwable (you dont want to be thrown a 20kg chainsaw)
  • Lethal (you dont want a soft toy)

A relationship between a class and an interface is of type "has-a".

<?php

class Knife implements Throwable, Lethal {
    
    public function throw() {
        return true;
    }
    
    public function kill() {
        return true;
    }
}

interface Throwable {
    public function throw();
}

interface Lethal {
    public function kill();
}

Abstract Class

Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no implementation. Abstract classes may not be instantiated, and require subclasses to provide implementations for the abstract methods.

Imagine a human, we can walk, defend and eat food. Comparing man and the average zombie, we all eat food - just zombies tend to eat other humans. But zombies behaviour differs in that they are dead, and do not defend themselves, they just ruthlessly attack. When behaviour is different, it is a good candidate for an abstract method (forcing subclasses to provide a custom implementation).

A relationship between a class and an abstract class is of type "is-a"

class Man extends Human {

    public function isAlive() {
        return true;
    }

    public function defend() {
        return true;
    }
}
class Zombie extends Human {

    public function isAlive() {
        return false;
    }

    public function defend() {
        return false;
    }
}

abstract class Human {
    abstract function defend();
    abstract function isAlive();

    public function eat(Food $food) {
        echo "Mmmm tasty";
    }
}

Program to an interface, not an implementation

You should always think about the interface when implementing your code, the advantage is that clients remain unaware of the specific types of objects, as long as the objects adhere to the interface. This leads to simplifed code and greatly reduced dependancies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment