how to inherit a class php

Solutions on MaxInterview for how to inherit a class php by the best coders in the world

showing results for - "how to inherit a class php"
Issa
23 Jun 2020
1
2I think the best way for beginners to understand inheritance is through a real example so here is a simple example I can gave to you 
3
4<?php
5
6class Person
7{
8    public $name;
9    protected $age;
10    private $phone;
11
12    public function talk(){
13        //Do stuff here
14    }
15
16    protected function walk(){
17        //Do stuff here
18    }
19
20    private function swim(){
21        //Do stuff here
22    }
23}
24
25class Tom extends Person
26{
27    /*Since Tom class extends Person class this means 
28        that class Tom is a child class and class person is 
29        the parent class and child class will inherit all public 
30        and protected members(properties and methods) from
31        the parent class*/
32
33     /*So class Tom will have these properties and methods*/
34
35     //public $name;
36     //protected $age;
37     //public function talk(){}
38     //protected function walk(){}
39
40     //but it will not inherit the private members 
41     //this is all what Object inheritance means
42}
43
44