Interface with not-required methods

10,307

Solution 1

No. The whole idea of interfaces is to have a contract that guarantees that a method exists.

But a class can implement multiple interfaces, so you can define a different interface that contains that method and not add that interface to the class that doesn't have the method.

Solution 2

Interfaces cannot have optional methods. That's the concept behind interface. If you however need something optional, then I suggest to additionally create default implementation of your interface which then all classes you need would then extend. This way all of these classes would implement interface and you would also be able to override just selected methods, having your optional behaviour.

Something like that:

interface MyInterface {
  public function method1();
  public function method2();
}

then Base class implements your interface's methods (I made it abstract to disallow direct use):

abstract class Base implements MyInterface {
    public function method1() {
       // dummy
    }
    public function method2() {
       // dummy
    }
}

and then:

class Optional extends Base {
   // method1 is not overridden, so Base' implementation applies

   public function method2() {
     // something here
   }
}

Solution 3

Please see example here:

interface Workable
{
    public function work();
}

interface Feedable
{
    public function eat();
}

interface Employee extends Feedable, Workable
{
}

class Human implements Employee
{
    public function work()
    {
        // ....working
    }

    public function eat()
    {
        //.... eating in lunch break
    }
}

// robot can only work
class Robot implements Workable
{
    public function work()
    {
        // ....working
    }
}

Source: https://github.com/jupeter/clean-code-php

Solution 4

I found an intresting library introducing WeakInterfaces. However I don't think it would be easy to make it work with Doctrine.

Share:
10,307
forsberg
Author by

forsberg

Updated on June 04, 2022

Comments

  • forsberg
    forsberg almost 2 years

    Is there a way to indicate optional method in the interface (so that the contract only indicated the number / type of arguments to be given)?

    Please give maybe a little more understanding and insight into the problem, and indicate a solution? See for instance this discussion: Optional Methods in Java Interface

    In the app I'm using Listeners connected to the Persistence (Doctrine). So I'm using some of these methods:

    prePersist()
    preUpdate()
    postPersist()
    postUpdate()
    

    etc.

    Now, while refactoring, since there are too many Entities (objects to be persisted) I decided to split the parts of these methods into separate classes.

    However not all of them need all pre-... and post-... methods. I need to make sure they are given appropriate number and type of arguments. How do you do that in PHP?

  • forsberg
    forsberg over 9 years
    See my update. I'm not going to create a number of interfaces with pre- and post- methods. It's quite ridiculous, don't you think? ;)
  • GolezTrol
    GolezTrol over 9 years
    No I don't think so, but another solution would be to just add an empty implementation of the function. You could consider adding empty implementations in an Entity base class and only override these when needed. Anyway, you must have the implementation if the interface declares the function. It must be said though, that PHP isn't that strict anyway, and the (lack of) enforcement of interface rules is ridiculous.
  • forsberg
    forsberg over 9 years
    Ok, thanks for suggestion. I decided to go in large part the mentioned Java-discussion way, with a single interface.
  • Niklas Ekman
    Niklas Ekman about 6 years
    Interfaces should not be split into "artificial" containers, they should be split "logically". Interfaces are used to create proper logical abstractions in apps. It's up to the implementing class if it chooses to implement one or more interfaces.