WBP22202A0023PR08
WBP22202A0023PR08
Experiment No: 8
Title of Experiment Write a PHP program to :
a) Inherit members of superclass in subclass.
b) Create constructor to initialize object of class by using object-oriented
concepts.
return $this->message;
☆ Exercise
Ans:
<?php
class Person {
public $name;
public $age;
// Parameterized Constructor
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
public function display() {
echo "Name: " . $this->name . "<br>";
echo "Age: " . $this->age . "<br>";
}
}
// Creating objects with parameters
$person1 = new Person("Alice", 25);
$person2 = new Person("Bob", 30);
// Display details
$person1->display();
$person2->display();
?>
Output: