sudhakarinfotech
(current)
Run Code
PHP Code Editor-
PHP Source Code
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PHP OOP Inheritance Using Protected Access Modifier</title> </head> <body> <h1>PHP OOP Inheritance Using Protected Access Modifier</h1> <?php class Fruit { protected $name; protected $color; public function __construct($name, $color) { $this->name = $name; $this->color = $color; } public function basicInfo() { echo "The fruit is {$this->name} and the color is {$this->color}."; } } // Strawberry is inherited from Fruit class Mango extends Fruit { public function displayMessage() { echo "I have ".$this->name." having its color ".$this->color; } } $mango = new Mango("mango", "yellow"); $mango->displayMessage(); //invalid statement echo $mango->name; echo $mango->color; ?> </body> </html>
Source Code Output