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

Samarth Polytechnic Belhe: Department of Computer Engineering

This document discusses two PHP programming concepts: inheritance and constructors. 1) Inheritance allows a child class to inherit properties and methods from a parent class. The document provides examples of single, multilevel, and hierarchical inheritance in PHP. 2) A constructor initializes an object's properties upon creation and is defined using __construct(). The constructor examples show initializing object properties, which reduces code.

Uploaded by

Shekhar Jadhav
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)
56 views4 pages

Samarth Polytechnic Belhe: Department of Computer Engineering

This document discusses two PHP programming concepts: inheritance and constructors. 1) Inheritance allows a child class to inherit properties and methods from a parent class. The document provides examples of single, multilevel, and hierarchical inheritance in PHP. 2) A constructor initializes an object's properties upon creation and is defined using __construct(). The constructor examples show initializing object properties, which reduces code.

Uploaded by

Shekhar Jadhav
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

Samarth Polytechnic Belhe

DEPARTMENT OF COMPUTER ENGINEERING

Subject : Web Based application development using PHP. Subject code : 22619

Semester : 6th Course : Computer Engineering

Name of Subject Teacher: Prof . Kshirsagar . S. B Laboratory No:L001B

Name of Student: Jagtap Sarthak Santosh Enrollment no:- 1909920061


Date of Performance : / /2022 Date of Submission : / /2022

Experiment No: 8

Title of Experiment: Write PHP program to


a.To inherit the member of super class in
subclass
b.Create a constructor to initialize object
of a class.

Theory:
1.0 Title:

Write PHP program to


a. To inherit the member of super class in subclass
b. Create a constructor to initialize object of a class.
2.0 Inheritance
Deriving new class from existing class is called inheritance. The child class will inherit
all the public and protected properties and methods from the parent class. In
addition, it can have its own properties and methods.

3.0 New Concepts: Types


of Inheritance:
1. Single Inheritance
class A extends B
{
…………………………
…………………………
}
2. Multilevel
inheritance
class A
extends B
{
…………………………
………………………
}
class C extends A
{
……………………..
…………………………
}

3. Hierarchical
inheritance class A
extends B
{
…………………………
………………………
}
class C extends B
{
……………………..
…………………………
}

4. Multiple inheritance
Not supported by PHP. But it can be achieved using trait keyword.

 Program:-

<?php
class Fruit {
public $name;
public $color;
public function construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
// Strawberry is inherited from Fruit
class Strawberry extends Fruit {
public function message() {
echo "Am I a fruit or a berry? ";
}
}

$strawberry = new Strawberry("Strawberry", "red");


$strawberry->message();
$strawberry->intro();
?>

 Output:-

Constructor:

 A constructor allows you to initialize an object's properties upon creation of the


object. In PHP, constructor is defined using construct() .
 construct function starts with two underscores ( )
 a constructor saves us from calling the set_name() method which reduces the
amount of code
 construct() function that is automatically called when you create an object from a
class

 program:-
<?php
class Car{
public $name;
public $color;
function construct($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}

$Porsche = new Car("Porshe");


echo $Porsche->get_name();
?>

 Output:-

Process Product Total Dated Sign Of


Grade and Related Related (25 M) Teacher
Dated Signature of (15M) (10M)
Teacher

You might also like