What is the significance of declaring a method in a derived class in PHP?
Declaring a method in a derived class in PHP allows you to override the method from the parent class with a new implementation. This is useful when you want to customize the behavior of a method for a specific subclass without affecting the parent class or other subclasses. By declaring a method in a derived class, you can provide a specialized implementation that is tailored to the needs of that particular subclass.
class ParentClass {
public function sayHello() {
echo "Hello from ParentClass";
}
}
class ChildClass extends ParentClass {
public function sayHello() {
echo "Hello from ChildClass";
}
}
$parent = new ParentClass();
$parent->sayHello(); // Output: Hello from ParentClass
$child = new ChildClass();
$child->sayHello(); // Output: Hello from ChildClass
Related questions
-
Syntax error PHP parser Debugging tools
-
API Hitbox.tv debugging
-
execute prepare statement