How should the keywords extends and implements be used in PHP classes to extend functionality?
When we want to extend the functionality of a class in PHP, we can use the "extends" keyword to create a subclass that inherits properties and methods from a parent class. If we want to implement interfaces in a class, we can use the "implements" keyword to specify which interfaces the class should adhere to. By using these keywords, we can easily extend and implement additional functionality in our PHP classes.
// Parent class
class ParentClass {
// Properties and methods
}
// Subclass extending ParentClass
class SubClass extends ParentClass {
// Additional properties and methods
}
// Interface
interface MyInterface {
// Method declarations
}
// Class implementing MyInterface
class MyClass implements MyInterface {
// Method implementations
}