php exp8 23
php exp8 23
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
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.
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;
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
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