0% found this document useful (0 votes)
5 views4 pages

WBP22202A0023PR08

Uploaded by

guyn9074
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)
5 views4 pages

WBP22202A0023PR08

Uploaded by

guyn9074
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/ 4

IF6IA WBP

DEPARTMENT OF INFORMATION TECHNOLOGY

Subject: Web based Application Development Subject Code: 22619


using PHP
Semester: 6th Course: IF6IA
Laboratory No: L001 Name of Teacher: Chetashri Bhusari
Name of Student: Prajwal Patekar Roll ID: 2220A0023

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.

☆ Practical Related Questions


1. How to create a sub class.
Ans: A subclass (also called a child class) in PHP is created using inheritance, which allows a
class to inherit properties and methods from another class (parent class). The extends keyword is
used to define a subclass.
class ParentClass {

public $message = "Hello from Parent Class";

public function showMessage() {

return $this->message;

class ChildClass extends ParentClass {

public function showChildMessage() {

return "This is from the Child Class";

// Create an instance of ChildClass

$child = new ChildClass();

// Access parent class method

22202A0023 Prajwal Patekar


IF6IA WBP

echo $child->showMessage(); // Output: Hello from Parent Class

// Access child class method

echo $child->showChildMessage(); // Output: This is from the Child Class

2. Define inheritance and its type with diagram.


Ans: Inheritance is a fundamental Object-Oriented Programming (OOP) concept that allows a
child class to acquire the properties and behaviors (methods) of a parent class. This promotes code
reuse and a hierarchical class structure.
Types of Inheritance in PHP
1. Single Inheritance – One child class inherits from a single parent class.
2. Multilevel Inheritance – A child class inherits from another child class, forming a chain of
inheritance.
3. Hierarchical Inheritance – Multiple child classes inherit from the same parent class.
4. Multiple Inheritance – PHP does not support multiple inheritance directly, but it can be
achieved using Traits.

22202A0023 Prajwal Patekar


IF6IA WBP

☆ Exercise

1. Write a program to demonstrate parameterized constructor

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:

22202A0023 Prajwal Patekar


IF6IA WBP

2. Write a program to demonstrate default constructor.


Ans:
<?php
class Message {
public function __construct() {
echo "Welcome to PHP OOP!<br>";
}
}
// Creating an object of the class
$obj1 = new Message();
$obj2 = new Message();
?>
Output:

22202A0023 Prajwal Patekar

You might also like