0% found this document useful (0 votes)
5 views

php exp8 23

The document outlines a laboratory experiment for a web page development course using PHP, focusing on object-oriented programming concepts such as inheritance and constructors. It includes example PHP code demonstrating class creation, object instantiation, and method usage, along with practical questions about class-object relationships and keywords like $this and self. Additionally, it discusses the limitations of multiple inheritance in PHP and provides a Python example of inheritance implementation.

Uploaded by

aafu202
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

php exp8 23

The document outlines a laboratory experiment for a web page development course using PHP, focusing on object-oriented programming concepts such as inheritance and constructors. It includes example PHP code demonstrating class creation, object instantiation, and method usage, along with practical questions about class-object relationships and keywords like $this and self. Additionally, it discusses the limitations of multiple inheritance in PHP and provides a Python example of inheritance implementation.

Uploaded by

aafu202
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

DEPARTMENT OF COMPUTER ENGINEERING

Subject: web page development using php Subject Code: 22619


Semester: 6 Course: CO6I-A
Laboratory No: Name of Subject Teacher: Saqib Ghatte
Name of Student: AFIF IRFAN NAZIR Roll Id: 21203A1005

Experiment No: 8
Title of Experiment Write a simple PHP program to
a. Inherit members of super class in subclass.
b. Create constructor to initialize object of class by using object-oriented
concepts

Program code:
1. <?php
class Shape
{
public $length;
public $width;
public function __construct($length, $width)
{
$this->length = $length;
$this->width = $width;
}
}
class Rect extends Shape
{
public $height;
public function __construct($length, $width, $height)
{
$this->length = $length;
$this->width = $width;
$this->height = $height;
}
public function intro()
{
echo "The length is {$this->length}, the width is {$this->width}, and the height is
{$this->height} ";
}
}
$r = new Rect(10,20,30);
$r->intro();
?>
Output :
The length is 10, the width is 20, and the height is 30

2. <?php
class Employee
{
public $name;
public $position;
function __construct($name,$position)
{
// This is initializing the class properties
$this->name=$name;
$this->profile=$position;
}
function show_details()
{
echo $this->name." : ";
echo "Your position is ".$this->profile."<br>";
}
}
$employee_obj= new Employee("Prasad Koyande","developer");
$employee_obj->show_details();
$employee2= new Employee("Vijay Patil","Manager");
$employee2->show_details();
?>
Output :
Prasad Koyande : Your position is developer
Vijay Patil : Your position is Manager
Practical related questions

1. Define the relationship between a class and an object?


In object-oriented programming, a class is a blueprint or a template for creating objects,
while an object is an instance of a class.

A class defines the properties and behaviors of a certain type of object, while an object
is a specific instance of that class with its own set of values for the properties.

For example, if we have a Car class, it may define properties such as make, model, and
year, as well as behaviors such as drive(), stop(), and accelerate(). When we create an
object of this class, we can set values for the properties (e.g. Toyota, Corolla, and 2022)
and call the behaviors (e.g. $myCar->drive()).

In PHP, we can define a class using the class keyword, and create an object of that class
using the new keyword.

2. What is the purpose of $this & extends?


In PHP, $this is a keyword that refers to the current object instance within a class
method. It can be used to access the object's properties and methods from within the
class.
On the other hand, extends is a keyword used in PHP to create a new class as a child of
an existing class, also known as inheritance. The child class inherits all the properties
and methods of the parent class, and can also override or add new properties and
methods.

3. State the use of ‘self’ with suitable example.


In PHP, self is a keyword that refers to the current class within a class definition. It can
be used to access the class constants, static properties, and static methods from within
the class.

For example, consider the following Vehicle class that defines a constant
WHEEL_COUNT and a static property count:
class Vehicle {
const WHEEL_COUNT = 4;
public static $count = 0;

public function __construct() {


self::$count++;
}
}
$car1 = new Vehicle();
$car2 = new Vehicle();
$car3 = new Vehicle();
echo Vehicle::WHEEL_COUNT; // Output: 4
echo Vehicle::$count; // Output: 3

Exercise
1. How to implement multiple inheritance in PHP?
In PHP, multiple inheritance is not supported, i.e., a class cannot inherit from multiple classes
at the same time. However, we can simulate multiple inheritance in PHP using traits.
A trait is a set of methods that can be reused in multiple classes. We can define a trait with
the trait keyword, and then use the use keyword to include it in a class.
trait A {
public function methodA() {
echo "Method A from trait A\n";
}
}
trait B {
public function methodB() {
echo "Method B from trait B\n";
}
}
class C {
use A, B;
}
$c = new C();
$c->methodA(); // Output: Method A from trait A
$c->methodB(); // Output: Method B from trait B

2. Implement following inheritance:


class Square:
def __init__(self, length):
self.length = length
self.breadth = length

class Rectangle(Square):
def __init__(self, length, breadth):
super().__init__(length)
self.breadth = breadth

def rectarea(self):
return self.length * self.breadth

class Box(Rectangle):
def __init__(self, length, breadth, height):
super().__init__(length, breadth)
self.height = height

def volume(self):
return self.length * self.breadth * self.height

You might also like