πŸš€ DevOps & SRE Certification Program πŸ“… Starting: 1st of Every Month 🀝 +91 8409492687 πŸ” Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

How to declare Inheritance in PHP with example?

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
?>
Output

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;
?>
Output

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;
?>

Output

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.