PR 8 PHP
PR 8 PHP
<?php {
class student echo $this->name ."<br>";
{ echo $this->rollno ."<br>";
public $name; }
public $rollno; }
function __construct($x,$y) $s=new student("smruti",10);
{ $s->show();
$this->name=$x; ?>
$this->rollno=$y; Output:
} smruti
10
function show()
---------------------Q2------------------------
<?php }
class demo }
{ $s=new demo();
function __construct() ?>
{ Output:
echo "this is constructor<br>"; this is constructor
------------------------single level inheritance-----------------
<?php }
class A }
{ $bb=new B();
public $a=10; $bb->show1();
function show1() echo "<br>";
{ $bb->show2();
echo "this is class1"; echo "<br>";
} echo $bb->a;
} echo "<br>";
class B extends A echo $bb->b;
{ ?>
public $b=20; Output:
function show2() this is class1
this is class2
{
10
echo "this is class2"; 20
----------------------multilevel inheritance--------------------
<?php {
class GrandFather echo "this is intermidiate class <br>";
{ }
function show1() }
{ class child extends Father
echo "this is base class<br>"; {
} function show3()
} {
class Father extends GrandFather echo "this is child class "; }}
{ $c=new child();
function show2() $c->show1();
$c->show2(); this is base class
this is intermidiate class
$c->show3();
this is child class
?>
Output:
---------------------multiple inheritance---------------------------
<?php }
class A class C extends A
{ {
function show1() use B;
{ function show3()
echo "welcome to function1<br>"; {
} echo "welcome to function3";
} }
trait B }
{ $c1=new C();
function show2() $c1->show1();
{ $c1->show2();
echo "welcome to function2<br>"; $c1->show3();
} ?>
Output:
welcome to function1
welcome to function2
welcome to function3
---------------------------------------second way of multiple-----
<?php interface B
class A{ {
function show1(){ public function show2();
echo "welcome to function1<br>";} }
} class C extends A implements B
{ $c1=new C();
function show2() $c1->show1();
{ $c1->show2();
echo "welcome to function2<br>"; $c1->show3();
} ?>
function show3() Output:
{ welcome to function1
welcome to function2
echo "welcome to function3";
welcome to function3
}
}
--------------------hierarchical inheritance-----------------------
<?php function show3()
class GrandFather {
{ echo "this is uncle class ";
function show1() }
{ }
echo "this is grandfather class<br>"; $f=new Father();
} $f->show1();
} $f->show2();
class Father extends GrandFather $u=new uncle();
{ $u->show1();
function show2() $u->show3();
{ ?>
echo "this is father class <br>"; Output:
} this is grandfather class
} this is father class
this is grandfather class
class uncle extends GrandFather this is uncle class
{