Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Name: Balsaraf Disha Sampat

Roll no: 06
Batch: C1
Practical no: 5

Q. Write a PHP program to


a) Inherit members of super class in subclass.
<html>
<head>
<title> Inheritance</title>
</head>
<body>
<?php
class collage
{
protected $name;
protected $code;
function set_collage_Info($name,$code)
{
$this->name=$name;
$this->code=$code;
}
}
class result extends collage
{
public $dname;
public $no_student;
function set_dept_info($dname,$no_student)
{
$this->dname=$dname;
$this->no_student=$no_student;
}
function displayInfo()
{
echo "Name of collage:
$this->name";
echo "<br>Institute code: $this->code";
echo "<br>Department name: $this->dname";
echo "<br>No of student in department: $this->no_student";
}
}
$var=new result();
$var->set_collage_Info("Jaihind Polytechnic","0508");
$var->set_dept_info("Computer","300");
$var->displayInfo();
?>
</body>
</html>

Output:

b) Create constructor to initialize object of class by using object-oriented concepts.


<html>
<head>
<title> Constructor</title>
</head>
<body>
<?php

class person
{
public $name;
public $age;
function person()
{
echo "This is constructor";
}
function __construct($n,$a)
{
$this->name=$n;
$this->age=$a;
}
function display()
{
echo "<br> $this->name $this->age";
}
}
$p=new person("Disha",19);
$p->display();

?>
</body>
</html>
<?php

Output:

You might also like