[Solved] Can we create abstract class without abstract method in php? [closed]


An abstract class is a class that cannot be instantiated and must be extended by child classes. Any abstract methods in an abstract class must be implemented by an extending child class (or it must also be abstract). It’s possible to have an abstract class without abstract methods to be implemented; but that’s not particularly useful.

The point of an abstract class is that you can define the interface in advance, but leave the implementation up to others. You can then work with that abstract interface and be sure it’ll work as intended, without knowing in advance what the concrete implementation will be:

abstract class Foo {
    abstract public function bar();
}

function baz(Foo $foo) {
    echo $foo->bar();
}

baz can depend on what $foo will look like exactly, without having any idea how it’s implemented exactly.

Having an abstract class without any abstract methods allows you to type hint Foo, but then you still don’t know anything about what $foo will have in terms of methods or properties, so it’s rather pointless.

1

solved Can we create abstract class without abstract method in php? [closed]