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
Loading...
Related Quiz
- PHP is case-sensitive for variable names.
- How can you filter multiple inputs in PHP?
- You are writing a PHP script and you need to check if a string matches a specific pattern. How would you do this using Regular Expressions in PHP?
- The fopen() function is used to open a file in PHP.
- In PHP, you can define a constant in a class using the const keyword like const CONSTANT_NAME = ______.