0% found this document useful (0 votes)
40 views3 pages

Practical No 2 (WBP) 06

This document contains code for two PHP programs demonstrating object-oriented programming concepts. The first program shows inheritance by creating a subclass that inherits properties and methods from a superclass. The subclass is able to access and display inherited properties. The second program creates a constructor that initializes object properties when a new object is created, allowing the properties to be set directly during object creation. Both programs output the property values to demonstrate the concepts.

Uploaded by

dishabalsaraf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views3 pages

Practical No 2 (WBP) 06

This document contains code for two PHP programs demonstrating object-oriented programming concepts. The first program shows inheritance by creating a subclass that inherits properties and methods from a superclass. The subclass is able to access and display inherited properties. The second program creates a constructor that initializes object properties when a new object is created, allowing the properties to be set directly during object creation. Both programs output the property values to demonstrate the concepts.

Uploaded by

dishabalsaraf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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