How can inheritance in PHP be utilized effectively to pass values between classes?
Inheritance in PHP allows for passing values between classes by creating a parent class with properties or methods that can be inherited by child classes. By utilizing inheritance, child classes can access and manipulate the values passed down from the parent class, providing a convenient way to share data and functionality across related classes.
class ParentClass {
protected $sharedValue;
public function __construct($value) {
$this->sharedValue = $value;
}
}
class ChildClass extends ParentClass {
public function getValue() {
return $this->sharedValue;
}
}
// Usage
$parent = new ParentClass("Hello");
$child = new ChildClass("World");
echo $child->getValue(); // Output: World