PHP oops concept :
One of the main advantages of object-oriented programming is the ability to reduce code duplication with inheritance,
code duplication means reduces same function/method write multiple time.code duplication occurs when same code write more than one.
In inheritance we have one parent class and other is child class, parent class having properties can access child class.
Code :
Demo:
How to create classes?
In order to create a class, we group the code that handles a certain topic into one place. For example, we can group all of the code that handles the users of a blog into one class, all of the code that is involved with the publication of the posts in the blog into a second class, and all the code that is devoted to comments into a third class.
Syntax:
class <Class-name>()
{
//write something code::-
}
Eg:
class MyCompany
{
$empID=”10”;
$empName=”Disuza Jen”;
$empMobile=”1234567890”;
}
Object of class create :
we can use object of class and access all properties of thos class.
In order to work with a class, we need to create an object from it. In order to create an object, we use the new keyword.
For example:
$objName=new MyCompany;
how to extend child class from parent class :
Example :
Example :
class child extend MyCompany
{
//Write code here :
}
Following basic example of company and company manufacture four wheeler,
Example :
<?php class Car
{
private $model;
public function CarModels($model)
{
$this->model->$model;
}
public function CarShow()
{
return “Model is : ”.$this->model;
}
}
class CarType extend Car
{
//no need code in child class::-
}
$carttype=new CarType();
$carttype-> CarModels(“Maruti”);
echo $carttype->hello();?>
Example run as :
please create following file strcture for implementing inheritance :
1-header.php,
2-footer.php,
3-index.php,
4-inderitance.js,
5-ChildInherit.php,
6-error.css,
7-jquery.validate.js,
1-header.php :
header file include following html code, header file link only boostrap and validation file link,
code :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>PHP Inheritance Program</title> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"> <link href="error.css" rel="stylesheet"> <script src="https://code.jquery.com/jquery-3.1.1.js" integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA=" crossorigin="anonymous"> </script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <script src="jquery.validate.js"></script> <script src="inderitance.js"></script> </head> <body> <!-- start:header --> <nav class="navbar navbar-default navbar-fixed-top"> <div class="container-fluid"> <!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header"> <a class="navbar-brand" href="#">Free Teachnology</a> </div> <!-- Collect the nav links, forms, and other content for toggling --> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li class="active"><a href="#">Home <span class="sr-only"></span></a></li> <li><a href="#">About us</a></li> <li><a href="#">Contact us</a></li> </ul> </div><!-- /.navbar-collapse --> </div><!-- /.container-fluid --> </nav>>
2-footer.php :
footer file include social media link :
Code :
<!-- Start::footer --> <footer> <div class="footer footer-bottom"> <div class="container"> <p class="pull-left"> Copyright © <?php echo date('Y');?>. Design and Develop by - Disuza Jen. </p> <div class="pull-right"> <ul class="nav nav-pills payments"> <li><i class="fa fa-facebook"></i></li> <li><i class="fa fa-google"></i></li> <li><i class="fa fa-twitter"></i></li> <li><i class="fa fa-link"></i></li> </ul> </div> </div> </div> <!--/.footer-bottom--> </footer> <!-- End::footer --> <link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css"> <br> <!--Start:: if success ajax call then show message --> <div id="demo"></div> <!--End:: if success ajax call then show message --> </body> </html>
3-inderitance.js :
it files include validation code of jquery files,
select box validation and if form submit successfully then ajax function call and show result as per select option select.
Code :
jQuery(document).ready(function () { $("#signupFrom").validate({ rules: { carbrand: { required: true, }, carmodel: { required: true, }, carcolour: { required: true, }, }, messages: { carbrand: { required: "* Please select car brand.", }, carmodel: { required: "* Please select car model.", }, carcolour: { required: "* Please select car colour.", }, }, submitHandler : function(form) { var carbrand = $.trim($('#carbrand').val()); $.ajax({ type: "POST", url: "ChildInherit.php", async: true, data: {carbrand:carbrand}, success: function(html) { var result=$.parseJSON(html); if(result.status==0) { $("#shows").hide(); alert('Something goes wrong.'); alert('Enter correct value.'); } else if(result.status==1) { $("#shows").show(); $("#carbrand_name").html(result.carname); $("#carmodel").html(result.car_model); $("#car_colour").html(result.car_colour); } else { alert('Something goes wrong.'); } } }); } }); });
submit handler function handle form submit or not, following function of ajax show response -
var carbrand = $.trim($('#carbrand').val()); $.ajax({ type: "POST", url: "ChildInherit.php", async: true, data: {carbrand:carbrand}, success: function(html) { var result=$.parseJSON(html); if(result.status==0) { $("#shows").hide(); alert('Something goes wrong.'); alert('Enter correct value.'); } else if(result.status==1) { $("#shows").show(); $("#carbrand_name").html(result.carname); $("#carmodel").html(result.car_model); $("#car_colour").html(result.car_colour); } else { alert('Something goes wrong.'); } } });
above js file ajax file reponse result status check 1 and 0 if status is get 1 then set value of carmodel,carbrand and car colour.
4-ChildInherit.php :
ChildInherit.php file is form submit action file call :
Code :
<?php /* *Start:: inheritance of car and car model with colour,brand, Developed by : disuza jen Mail id: disuzajen@gmail.com * */ //Post value ::- $carbrand_post=$_POST['carbrand']; class Car { public function carBrand() { $carbrand=array('Maruti','Mahindra','BMW','SKODA'); return $carbrand; } public function Maruti() { $maruti=array('color'=>'pink','model'=>'D4'); return $maruti; } public function Mahindra() { $Mahindra=array('color'=>'white','model'=>'A1'); return $Mahindra; } public function BMW() { $Mahindra=array('color'=>'Pink','model'=>'C1'); return $Mahindra; } public function SKODA() { $Mahindra=array('color'=>'Black','model'=>'B1'); return $Mahindra; } } class ChildInherit extends Car { } $obj =new ChildInherit(); $final= $obj->carBrand(); if(in_array($carbrand_post, $final)){ $finals= $obj->$carbrand_post(); $result=array('status'=>1,'carname'=>$carbrand_post,'car_colour'=>$finals['color'],'car_model'=>$finals['model']); echo json_encode($result); exit(); } else { $result=array('status'=>"0"); echo json_encode($result); exit(); } ?>
Now above example show inheritance of class mail class is Car and child class ChildInherit this call inhert form car and access all porperties of particular class :
Parent class code is :
class Car { public function carBrand() { $carbrand=array('Maruti','Mahindra','BMW','SKODA'); return $carbrand; } public function Maruti() { $maruti=array('color'=>'pink','model'=>'D4'); return $maruti; } public function Mahindra() { $Mahindra=array('color'=>'white','model'=>'A1'); return $Mahindra; } public function BMW() { $Mahindra=array('color'=>'Pink','model'=>'C1'); return $Mahindra; } public function SKODA() { $Mahindra=array('color'=>'Black','model'=>'B1'); return $Mahindra; } }
parent class define car brand name and brand name method define properties as per car brand model select wise. Main model Maruti,Mahindra,BMW,SKODA,
child class is and child class extends form parent class :
Code :
class ChildInherit extends Car
{
}
child class create object and thoes object call parent class method and properties ,
Code :
$obj =new ChildInherit();
$final= $obj->carBrand();
using object you can access parent class of function. I hope that you can understand and enjoy my code if you like my code please comment
Comments