
What is Inheritance?:-
Inheritance means one class method or properties access to another class. The child class will inherit all public, private, protected methods or properties from the parent class.
Public Access Modifier:-
<?php | |
//Public Access Modifier | |
class Mobile{ | |
public $a; //access can anywhere | |
public function displayParent(){ | |
echo "Parent construct Called $this->a <br>"; //This is parent construct called below in child construct | |
// $this->a = $z; | |
// echo ; | |
} | |
} | |
// $obj = $; | |
// $obj = new mobile; | |
// $obj->a = 20; | |
// $obj->display(); | |
class Nokia extends Mobile{ //This is child function extend parent function | |
public function displayChild($z){ | |
parent::displayParent(); //Calling parent function for print | |
$this->a = $z; | |
echo "This is child $this->a <br>"; | |
$this->displayParent(); | |
} | |
} | |
$obj = new Nokia; //This is object of child function | |
// $obj->a; | |
$obj->displayChild(20); //This is object of child function with arguments | |
?> |

Private Access Modifier:-
<?php | |
//private Access modifier | |
class Mobile{ | |
private $a; //This is Private Access Modifier | |
public function displayParent(){ | |
// $this->a = 30; | |
echo "This is parent $this->a"; | |
} | |
} | |
// $obj = new Mobile; | |
// $obj->a; //here, private Access modifier so, can't access this place | |
// $obj->displayParent(); | |
class Nokia extends Mobile{ | |
public function displayChild(){ | |
// $this->a = 30; | |
// echo "This is child $this->a"; | |
echo "This is private Access modifier in child class "; | |
} | |
} | |
$obj = new Nokia; | |
$obj->displayChild(); | |
// $obj->a; | |
?> |

Protected Access modifier :-
<?php | |
//Protected Access modifier | |
class Mobile{ | |
protected $a; //This is Protected Access Modifier | |
public function displayParent(){ | |
// $this->a = 30; | |
// echo "This is parent $this->a"; | |
} | |
} | |
// $obj = new Mobile; | |
// $obj->a; //here, protected access modifer so, access this place | |
// $obj->displayParent(); | |
class Nokia extends Mobile{ | |
// protected $a; | |
public function displayChild(){ | |
$this->a = '.'; //here, protected access modifer so, access this place | |
// echo "This is child $this->a"; | |
echo "This is Protected Access modifier in child class $this->a"; | |
} | |
} | |
$obj = new Nokia; | |
$obj->displayChild(); | |
// $obj->a; | |
?> |





Iβm a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND