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

Exp 08

The document outlines a PHP programming experiment focused on inheritance and constructors in object-oriented programming. It explains the concepts of inheritance, constructors, and destructors, providing example code for a Shape class and an Employee class. Additionally, it includes practical questions and exercises related to PHP functions.

Uploaded by

Sami Md
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)
6 views4 pages

Exp 08

The document outlines a PHP programming experiment focused on inheritance and constructors in object-oriented programming. It explains the concepts of inheritance, constructors, and destructors, providing example code for a Shape class and an Employee class. Additionally, it includes practical questions and exercises related to PHP functions.

Uploaded by

Sami Md
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

Experiment No: 08

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.

Resources required:
Hardware Software
Computer System Any database tools such as XAMPP

Practical Significance:

Inheritance:

- Inheritance is a mechanism of extending an existing class by inheriting a class.


- We create a new sub class with all functionality of that existing class, and we can add
new members to the new sub class.
- When we inherit one class from another we say that inherited class is a subclass and
the class who has inherits is called parent class.

Constructor and Destructor:


- A constructor and a destructor are special functions which are automatically called
when an object is created and destroyed.
- Constructors are special member functions for initialize variables on the newly
created object instances from a class.
- When creating a new object, it’s useful to set up certain aspects of the object at the
same time. For example, you might want to set some properties to initial values, fetch
some information from a database to populate the object, or register the object in
some way.
- Similarly, when it’s time for an object to disappear, it can be useful to tidy up aspects
of the object, such as closing any related open files and database connections, or
unsetting other related objects.
- An object’s constructor method is called just after the object is created, and its
destructor method is called just before the object is freed from memory.

Theoretical Background:
Inheritance:

To declare that one class inherits the code from another class, we use the extends
keyword.
Syntax :
class Parent
{
// The parent’s class code
}
class Child extends Parent
{
// The child can use the parent's class code
}
The child class can make use of all the non-private (public and protected) methods and
properties that it inherits from the parent class. This allows us to write the code only once in
the parent, and then use it in both the parent and the child classes.

Constructor and Destructor:


 Normally, when you create a new object based on a class, all that happens is that the
object is brought into existence. (Usually you then assign the object to a variable or pass
it to a function.) By creating a constructor method in your class, however, you can cause
other actions to be triggered when the object is created.
To create a constructor, simply add a method with the special name _ _construct() to
your class. (That ’ s two underscores, followed by the word “ construct, ” followed by
parentheses.) PHP looks for this special method name when the object is created; if it
finds it, it calls the method.

class MyClass
{
function __construct()
{
echo “Welcome to PHP constructor. <br / > ”;
}
}
$obj = new MyClass; // Displays “Welcome to PHP constructor.”

Program Code:

a)

<?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();
?>
b)
<?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();
?>
Practical related questions:
1. What is anonymous function?
2. Write the difference between built in function & user defined function.
3. What is variable function?

Exercise:
1. Write a code to perform addition of 3 numbers using function.
2. Write a PHP program to check whether number is even or odd using function.
3. Write a PHP program to print factorial of number using function.
4. Write PHP program to calculate the sum of digits using function.
5. PHP program to check whether a number is prime or Not using function.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy