What are the differences between an interface and a class in PHP?

  • Instantiation: An interface cannot be instantiated directly, while a class can be instantiated.
  • Method Implementation: An interface can only declare methods (without implementation), whereas a class can define both abstract methods and concrete methods.
  • Inheritance: A class can extend only one other class (single inheritance), but it can implement multiple interfaces.
  • Properties: An interface cannot contain properties, while a class can define properties.
  • All the options
Interfaces in PHP indeed define a contract for classes to adhere to, specifying the methods that implementing classes must implement. Interfaces cannot be instantiated directly and only provide method signatures without implementation. On the other hand, classes can be instantiated to create objects and can define both method signatures and their implementations. Classes can be inherited by other classes, while interfaces can be implemented by classes. These distinctions differentiate the role and purpose of interfaces and classes in PHP OOP. To know more, refer to: http://php.net/manual/en/language.oop5.interfaces.php
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *