What are some best practices for accessing methods of a parent class in PHP object-oriented programming?
When working with object-oriented programming in PHP, you may need to access methods of a parent class from a child class. To do this, you can use the `parent::` keyword followed by the method name to call the parent class method. This allows you to reuse code and avoid duplicating functionality in your child classes.
class ParentClass {
public function parentMethod() {
echo "This is a method from the parent class.";
}
}
class ChildClass extends ParentClass {
public function childMethod() {
parent::parentMethod();
}
}
$child = new ChildClass();
$child->childMethod(); // Output: This is a method from the parent class.