gprime
gprime

Reputation: 2353

An Interface with Abstract Methods

I came across some PHP code that was written by a co-worker (it was not used for anything). Basically it was an interface containing abstract methods. I then said that this was stupid and showed another co-worker sitting next to me. We laughed but then started to ask each other if it was possible and if so if it was actually useful. Apparently it is not possible (see example below), but if it was possible would it be useful.

Can you think of situations where this could be useful?

<?php
    interface Itest
    {
        abstract public function add(int $x, int $y);
    }

    abstract class ParentTest implements Itest
    {
        abstract public function add(int $x, int $y);
    }

    class test extends ParentTest
    {
        public function add(int $x, int $y)
        {
            return $x+$y;
        }
    }

    $w = new test;
    echo $w->add(5,8);
?>

Upvotes: 0

Views: 697

Answers (2)

Rafe Kettler
Rafe Kettler

Reputation: 76945

No, it's not useful. He should either use abstract classes or just plain interfaces.

Interface methods are basically abstract anyway, so having abstract interface methods doesn't make much sense.

Upvotes: 1

netcoder
netcoder

Reputation: 67685

All methods in an interface are abstract by definition.

An abstract method is a method for which the prototype is supplied but not implemented. It forces subclasses to implement it, or be declared abstract.

Upvotes: 3

Related Questions