Search code examples
phpoopstrategy-pattern

Is it okay for a subclass to implement an interface?


I am dipping my toes in more serious OOP (previously, just used inheritance) and I have stumbled across something that has confused me.

I have a super class that handles my database connection.

I have a subclass that handles connections related to a site's membership functions

I would like to employ the strategy pattern to allow different but similar functions to coexist. For example:

  • connect to database and check name and email for new registrations
  • connection to database and check username and confirmation code for registration confirmation
  • connect to database and check username and password for login

Solution

  • can an abstract class extend a super class in PHP?

    This

    class A {}
    abstract class B extends A {}
    class C extends B {}
    
    var_dump( new C );
    

    gives

    object(C)#1 (0) {}
    

    so the answer is: Yes, an abstract class can extend a super class in PHP.

    EDIT after title update:

    Yes, it is perfectly fine for a subclass to implement an interface. However, in the context of a Strategy you will likely not call the methods on any other interface than the interface that captures the abstraction and buries implementation details in derived classes. See http://sourcemaking.com/design_patterns/strategy