Practical 8
Single inheritance
<?php
class Base
{
function fun1()
{
echo "Prachi";
}
}
class Derived extends Base
{
function fun2()
{
echo "Hello! ";
}
}
$obj= new Derived();
$obj->fun2();
$obj->fun1();
?>
Output:
Hello! Prachi
Multilevel Inheritance
<?php
class Base
{
function fun1()
{
echo "welcome in php" ;
}
}
class Derived extends Base
{
function fun2()
{
echo "Hello prachi "."\n";
}
}
class Derived1 extends Derived
{
function fun3()
{
echo "Hello world "."\n";
}
}
$obj= new Derived1();
$obj->fun3();
$obj->fun2();
$obj->fun1();
?>
Output:
Hello world
Hello prachi
welcome in php
Hierarchical Inheritance
<?php
class college
{
public $collegename;
public function set()
{
$this->collegename="vapm almala";
echo $this->collegename."\n";
}
}
class student extends college
{
public $rollno;
public $name;
public function set1()
{
$this->rollno=10;
$this->name="prachi";
echo $this->rollno.$this->name."\n";
}
}
class teacher extends college
{
public $id;
public $name;
public function set2()
{
$this->id=100;
$this->name="abc";
echo $this->id.$this->name."\n";
}
}
$t=new teacher();
$s=new student();
$s->set();
$s->set1();
$t->set();
$t->set2();
?>
Output:
vapm almala
10prachi
vapm almala
100abc
Multiple Inheritance:
<?php
class A
{
public function show()
{
echo"class a","\n";
}
}
trait B
{
public function show2()
{
echo"trait b","\n";
}
}
class C extends A
{
use B;
public function show3()
{
echo"class c";
}
}
$c=new C();
$c->show();
$c->show2();
$c->show3();
Outout:
class a
trait b
class c
Constructor
Default constructor:
<?php
class student
{
public $roll;
public $name;
function __construct()
{
$this->roll=10;
$this->name="prachi";
echo $this->roll.$this->name;
}
}
$s=new student();
?>
Output:
10prachi
Paramiterized constructor:
<?php
class student
{
public $roll;
public $name;
function __construct($roll,$name)
{
$this->roll=$roll;
$this->name=$name;
echo $this->roll.$this->name;
}
}
$s=new student(10,"prachi");
?>
Output:
10prachi