Source Code:
(back to article)
<?php // Example 1 class Fruit { public $name; public $color; function __construct($name, $color) { $this->name = $name; $this->color = $color; } function getInfo() { echo "This fruit is a " . $this->name . " and it is " . $this->color . "."; } } class Apple extends Fruit { function getInfo() { echo "This fruit is an " . $this->name . " and it is " . $this->color . "."; } } $apple = new Apple("apple", "red"); $apple->getInfo(); // Output: This fruit is an apple and it is red. // Example 2 class Car { public $model; public $year; function __construct($model, $year) { $this->model = $model; $this->year = $year; } function getInfo() { echo "This car is a " . $this->model . " and it was made in " . $this->year . "."; } } class Toyota extends Car { function __construct($year) { parent::__construct("Toyota", $year); } } $toyota = new Toyota(2021); $toyota->getInfo(); // Output: This car is a Toyota and it was made in 2021.
Result:
Report an issue