Dev Diaries WD logo Dev Diaries WD
PHP OOP · EP 11
PHP OOP · Episode 11

OOP: Inheritance (extends & parent::)

Reuse a parent class inside a child class

ep11_oop_inheritance.php
<?php

class Animals {
        protected $name;
        protected $breed;

        public function __construct($name, $breed)
        {
            $this->name = $name;
            $this->breed = $breed;
        }

        public function getMeNameOfAnimal(){
            return "The name of animal is : $this->name";
        }

        public function generalName(){
            return "I am an animal";
        }
}

class Dog extends Animals{
    public function getMeBreedOfAnimal(){
         return "The breed of animal is : $this->breed";
    }

     public function generalName(){
            return "I am a Dog";
     }
}

$dogOne = new Dog("Maggie" , "Pug" );
Output
Output for this tutorial goes here.

The name of animal is : Maggie

The breed of animal is : Pug

I am a Dog

Watch the full episode

Watch on YouTube