r/PHPhelp • • Nov 17 '22

Solved PHP Abstract Classes, PHP Interfaces

I read about it online and still can't understand what these two is about. How does it work and when do you use these two? Can someone give example and explain these to me in simple words? Thanks!

8 Upvotes

9 comments sorted by

10

u/ZippyTheWonderSnail Nov 17 '22

An interface is simply a template for building a class. It does not contain code inside functions. It merely defines which need to appear in implemented classes. It's generally used to ensure all classes of a certain type obey a common naming convention.

Imagine you need to connect a database. The interface defines the functions classes can use to make database calls, regardless of the database. So even if the database changes (say from MySQL to PostgreSQL), it doesn't matter. The function names will be the same, and the data returned with be the same.

https://www.php.net/manual/en/language.oop5.interfaces.php

interface Template 
{
    public function doSomething(int $id): string
    public function doAnotherThing(string $email): bool
}

class myClass implements Template
{
    public function doSomething(int $id): string
    {
        // Do something and return a string
    }
    
    public function doAnotherThing(string $email): bool
    {
        // Do something and return a boolean (true / false)
    }
}

class mySecondClass implements Template
{
    public function doSomething(int $id): string
    {
        // Do something else and return a string
    }
    
    public function doAnotherThing(string $email): bool
    {
        // Do something else and return a boolean (true / false)
    }
}

1

u/[deleted] Nov 17 '22

Thankyou fot the effort guys! my eyes was opened.

2

u/stfcfanhazz Nov 17 '22

Abstract classes are useful when every implementation of them (every sub-class) shares some common behaviour. It saves you having to code the same or similar logic in every implementation. Interfaces define what methods a class needs to have, and it means in dependent code where you don't care what kind of implementation you get, you can type hint for the interface and you know what methods are available.

2

u/bkdotcom Nov 17 '22

an interface is akin to a "contract"..
If a class implements an interface, you know it contains the interface's methods. You can typehint and check against the interface vs some specific implementation

function __construct(SomeInterface $dependancy)

An abstract is simply a base class / building block that must be extended by a non-abstract class to be usable.. Good for defining base functionality...

2

u/williarin Nov 17 '22

Interface is the user manual of the chair, abstract class is the chair without the legs, class is the full chair.

3

u/stfcfanhazz Nov 17 '22

Not sure why this analogy is being downvoted I think it's nice

1

u/ZippyTheWonderSnail Nov 17 '22

An abstract class is like an interface with the ability to add functional code. So a class which extends an abstract class may have to implement functions, and may also inherit functions and properties.

https://www.php.net/manual/en/language.oop5.abstract.php

8

u/CyberJack77 Nov 17 '22

There are way more differences between interfaces and abstract classes, so to say an abstract class is like an interface is simply selling it short.

An interface can be seen as a blueprint that describes the public API. In other words, it describes all public methods that should be implemented when using the interface. The interface cannot contain protected and private methods and cannot contain properties or constants. A class can implement multiple interfaces, and each of the described public methods must be implemented in the class.

An abstract class is a class that cannot be instantiated by itself, and to use it, it must be extended. An abstract class can contain public, protected and private methods, abstract methods, properties and constants. The abstract class can extend another class and can also implement interfaces. A class can extend the abstract class and the "abstract" methods need to be implemented in the class itself. Any missing methods from applied interfaces must also be implemented in the class that extends the abstract class.

Since PHP does not support multiple inheritance, a class can only extend one other (abstract) class. This can cause some problems If you need methods from multiple (abstract) classes and things can become messy really quickly. There is a whole composition over inheritance discussion, but I don't think that is needed to explain the differences between an interface and an abstract class.

6

u/Breakdown228 Nov 17 '22

Also to note: An Abstract cant be instantiated like a normal class.