Sharp Tutorial
Inheritance in php

Python

Java

C

C++

HTML/CSS

Java Script

PHP

SQL

C Programs

PHP Introduction

  • PHP home
  • PHP introduction
  • How to install and run
  • Syntax and Rule in PHP
  • Data type and operator
  • If else in php
  • Loop in php
  • Array in php
  • Sring in php
  • Function in php
  • Include in php

Working with Forms

  • Get and Post
  • Get Form input into PHP
  • Form Validation

PHP with MySQl

  • Connect PHP & Mysql
  • Insert using PHP Mysql
  • Select using PHP MySql
  • Delete Update using PHP Mysql
  • Login Page Example in PHP

Advanced PHP

  • Session in PHP
  • Cookies in PHP
  • File Handling in PHP
  • File Upload/Download PH

Object Oriented PHP

  • Class Obejct in PHP
  • Constructor in PHP
  • Destructor in PHP
  • Inheritance in PHP

Inheritance in php

Inheritance is the property through which a subclass inherits the feature or property of its super class. It is also known as parent child class relationship.

In PHP inheritance is done with the help of extends keyword.

Example of Inheritance in PHP.

<?php

  class A
   { 
    public $i;
    function display() 
    {
     echo "base class=$this->i";
     }
   }
   
class B extends A // how B class inherits class A.
{
public $j;
function show()
{
echo "<br>subclass i=$this->i";
echo "<br>subclass j=$this->j";
}
}

 $a1= new A();
 $a1->i=100; // how to call properties or variable of a php class
 $a1->display();
 $b1= new B();
 $b1->i=200;
 $b1->j=400; 
 $b1->show(); 
?>

 

Output of the above program is as follows

Complete Example of Inheritance in PHP with constructor example

<?php
  class Person
   { 
    public $name;
     function __construct($n) //default constructor
    {
           $this->name=$n;
    }
    function display() //default function with no arguement.
    {
     echo "Person Details <br> Name:= $this->name" ;
     }
}

class Student extends Person
{
public $rollNo;
function __construct($n,$r)
{
$this->name=$n;
$this->rollNo=$r;
}
function print()
{
echo "<br>Student Details :<br>name= $this->name";
echo "<br> rollNo= $this->rollNo";
}
}
$p1= new person('tom');
$p1->display();
$s1= new Student('john',4563);
$s1->print();
?>

 

Output of the above program is as follows

Final class to prevent inheritance

<?php
 final class A
   { 
    
}

class B extends A
{
}
?>

 

above program output resulted in error as final class can not be inherited.

Enquiry about Course

Ask your Question

Click Here

Sharp (2) Tutorials

Video/ C Introduction

Watch video in full size

Video tutorial

Follow by Email
Facebook
Facebook
fb-share-icon
YouTube
Copyright © Sharp Tutorial
Build with WordPress
Wordpress Social Share Plugin powered by Ultimatelysocial
Sharp Tutorial