child class php

Solutions on MaxInterview for child class php by the best coders in the world

showing results for - "child class php"
Lara
08 Jan 2018
1<?php
2class Fruit {
3  public $name;
4  public $color;
5  public function __construct($name, $color) {
6    $this->name = $name;
7    $this->color = $color;
8  }
9  public function intro() {
10    echo "The fruit is {$this->name} and the color is {$this->color}.";
11  }
12}
13
14// Strawberry is inherited from Fruit
15class Strawberry extends Fruit {
16  public function message() {
17    echo "Am I a fruit or a berry? ";
18  }
19}
20$strawberry = new Strawberry("Strawberry", "red");
21$strawberry->message();
22$strawberry->intro();
23?>
24Example Explained
25The Strawberry class is inherited from the Fruit class.
26
27This means that the Strawberry class can use the public $name and $color properties as well as the public __construct() and intro() methods from the Fruit class because of inheritance.
28
29The Strawberry class also has its own method: message().