CONCEPT OF INHERITANCE IN PHP ALONG WITH EXAMPLE OF CONTACT US FORM OF INHERITANCE(VERY IMPORTANT TOPIC FOR INTERVIEW - QUESTIONS MAY BE ASKED AS WHAT IS INHERITANCE? EXPLAIN WITH AN EXAMPLE)
CONCEPT OF INHERITANCE IN PHP ALONG WITH EXAMPLE OF CONTACT US FORM USING INHERITANCE(VERY IMPORTANT TOPIC FOR INTERVIEW - QUESTIONS MAY BE ASKED AS WHAT IS INHERITANCE? EXPLAIN WITH AN EXAMPLE)
So, let's start with explaining about the concepts of inheritance. Do, you know what is inheritance? If you don't know. No problem. I will explain about the full concepts of inheritance. Ok, just let me explain about what is inheritance in brief. Inheritance is nothing rather I can say Inheritance is one of the method in OOP's (Object Oriented Programming). Inheritance always have two classes, one class is parent class and another class is child class. In Inheritance, the child class inherits or adhere all the properties and methods from his parent class or rather you can say another class which is the parent class. I can provide one nice example of Inheritance. Like as I have mother and father, so my mother and father are my parents and whather good or bad qualities my father or mother has means my parents has all that good or bad qualities I inherit from my mother and father means from my parents and here I am the child class and my mother and father are the parent class. So, this process is known as Inheritance.
EXAMPLE =>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Contact Us</title>
<!-- Add Bootstrap - 4 CDN -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
<style type="text/css">
.text-center{
color: brown;
background-color: yellow;
}
.form-group{
color: indigo;
background-color: green;
font-size: 25px;
font-weight: bolder;
}
</style>
</head>
<body>
<header class="text-center"><h1>Contact Us Form</h1></header>
<form method="post" class="form-group">
Username: <input type="text" name="username"><br>
Email: <input type="email" name="email"><br>
Message: <textarea type="text" name="message"></textarea><br>
<button type="submit" name="submit" id="submit" value="submit">Submit</button>
</form>
<?php
if (isset($_REQUEST['submit'])) {
$Username=$_REQUEST['username'];
$Email=$_REQUEST['email'];
$Message=$_REQUEST['message'];
class ContactForm
{
function __construct($Username,$Email,$Message)
{
$this->un=$Username;
$this->em=$Email;
$this->me=$Message;
}
}
class Registration extends ContactForm
{
function __construct($Username,$Email,$Message)
{
parent::__construct($Username,$Email,$Message);
}
function regisFun()
{
echo $this->un."<br>".$this->em."<br>".$this->me;
}
}
$obj=new Registration($Username,$Email,$Message);
$obj->regisFun();
}
?>
</body>
</html>
Here I have used parent::construct() as because the child class Registration is inheriting all the good or bad qualities rather you can say the properties or methods the child class Registration is inheriting from his parent class Contact Form.
OUTPUT OF THE CODE =>
Comments
Post a Comment