0% found this document useful (0 votes)
148 views

Unit - 3 Apply Object Oriented Concepts in PHP

The document discusses object oriented programming concepts like classes, objects, inheritance and interfaces in PHP. It explains how to create classes and objects in PHP, use constructors and destructors, implement inheritance through different inheritance types like single, multi-level, hierarchical and multiple inheritance using interfaces. It also provides examples to demonstrate these concepts.

Uploaded by

k9172056115
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
148 views

Unit - 3 Apply Object Oriented Concepts in PHP

The document discusses object oriented programming concepts like classes, objects, inheritance and interfaces in PHP. It explains how to create classes and objects in PHP, use constructors and destructors, implement inheritance through different inheritance types like single, multi-level, hierarchical and multiple inheritance using interfaces. It also provides examples to demonstrate these concepts.

Uploaded by

k9172056115
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Unit -3

Apply Object Oriented Concepts in


PHP

Mrs.Alinka sachin shinde


Lecturer in information technology
p.l.govt.polytechniclatur
Class and Object
 Write syntax to create class and object in PHP
➢ A class is defined by using the class keyword, followed by the name of
the class and a pair of curly braces ({}).
➢ A set of braces enclosing any number of variable declarations and
function definitions.
➢ In a class, variables are called properties and functions are called
methods!
Syntax:
<?php
class person
{
// code goes here...
}
?>
Example
 Declare a class named Student consisting of two properties ($name and $class) and two
methods set_name() and get_name() for setting and getting the $name property:
 <?php
class Student
{
// Properties
public $name;
public $class;
// Methods
function set_name($name)
{
$this->name = $name;
}
function get_name()
{
return $this->name;
}
}
?>
Object
 An object is an instance of class.
 The data associated with an object are called its properties.
 The functions associated with an object are called its methods.
 Object of a class is created by using the new keyword followed by classname.
 Classes are nothing without objects! We can create multiple objects from a class.
Each object has all the properties and methods defined in the class, but they will
have different property values.
 Syntax :
$object = new Classname( );
 Example:
$Obj=new Student(); or $Obj=new Student;
<?php
class Student
{
// Properties
public $name;
public $class;
// Methods
function set_name($name)
{
$this->name = $name;
}
function get_name()
{
return $this->name;
}
}
$obj= new Student();
$obj->set_name(‘Sonam');
echo $obj->get_name();
?>
Constructor and Destructor
 When we create an object of any class, we need to set properties of that
object before using it. We can do that by first initializing the object and
then setting values for the properties.
 To create and initialize a class object in a single step, PHP provides a
special method called as Constructor, which is used to construct the object
by assigning the required property values while creating the object.
 Once the object is initialized, the constructor is automatically called.
 The main purpose of this method is to initialize the object.
 Destructors are for destroying objects and automatically called at the end
of execution.
 PHP Destructor method is called and is about to release any object from its
memory. Generally, you can close files, clean up resources etc in the
destructor method.
 In PHP, we have special functions to define constructor and destructor for a
class, they are: __construct() and __destruct().
<?php
class <CLASS_NAME> {
// constructor
function __construct()
{
// initialize the object properties
}

// destructor
function __destruct()
{
// clearing the object reference
}
}
?>

Constructor can accept arguments, whereas destructors won't have any


argument because a destructor's job is to destroy the current object reference.
<?php
class Person
{
private $fname;
private $lname;
public function __construct($fname, $lname)
{
echo "Initialising the object...<br/>";
$this->fname = $fname;
$this->lname = $lname;
}
public function __destruct()
{
echo "Destroying Object...";
}
public function show()
{
echo "My name is: " . $this->fname . " " . $this->lname . "<br/>";
}
}
$x = new Person(“Ram", “Reddy");
$x->show(); Output:
?> My name is: Ram Reddy
Destroying Object...
Inheritance
 Inheritance is a fundamental idea in object-oriented programming.
 Inheritance in OOP = When a class derives from another class.
 Inheritance is process by which a child class can inherit all the properties and characteristics of the
parent class.
 Inheritance enables a class to use properties and methods of an existing class.
 The class which is inherited is called Parent class(or super class or base class) while the class which is
inheriting other class is called as Child class(or sub class or derived class).
 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.
 In PHP, extends keyword is used to define the child class.
 Inheritance allows a class to reuse the code from another class without duplicating it.
 Reusing existing codes serves various advantages. It saves time, cost, effort, and increases a
program’s reliability
 Syntax:
class derived_class_name extends base_class_name
{
// define member functions of
// the derived class here.
}
Example
<?php
class demo
{
public function display()
{
echo “Example of inheritance ";
}
}
class demo1 extends demo
{
public function view()
{
echo "in php";
}
}
$obj= new demo1();
$obj->display();
$obj->view(); Output:
?> Example of inheritance in php
Types of inheritance
1. Single Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance(Interfaces)
4. Hierarchical Inheritance

Single Inheritance
In a single inheritance, there is only one base class and one sub or derived class. It
directly inherits the subclass from the base class.
<?php
//PHP program to demonstrate the single inheritance.
class Base
{
function BaseFun()
{
echo "BaseFun() called<br>";
}
}
class Derived extends Base
{
function DerivedFun()
{
echo "DerivedFun() called<br>";
}
}
$dObj = new Derived();
$dObj->BaseFun();
$dObj->DerivedFun();
?>
Output
BaseFun() called
DerivedFun() called
Multi-Level Inheritance
In the multi-level inheritance, we will inherit the one base class into a derived class, and
then the derived class will become the base class for another derived class. <?php
//PHP program to demonstrate the multi-level inheritance.
class Base
{
function BaseFun()
{
echo "BaseFun() called<br>";
}
}
class Derived1 extends Base
{
function Derived1Fun()
{
echo "Derived1Fun() called<br>";
}
}
class Derived2 extends Derived1
{
function Derived2Fun()
{
echo "Derived2Fun() called<br>";
}
}
$dObj = new Derived2();
$dObj->BaseFun();
$dObj->Derived1Fun();
$dObj->Derived2Fun();
?>

Output
BaseFun() called
Derived1Fun() called
Derived2Fun() called
Hierarchical Inheritance
 In the hierarchical inheritance, we will inherit the one base class into multiple
derived classes.
<?php
//PHP program to demonstrate the hierarchical inheritance.
class Base
{
function BaseFun()
{
echo "BaseFun() called<br>";
}
}

class Derived1 extends Base


{
function Derived1Fun()
{
echo "Derived1Fun() called<br>";
}
}
class Derived2 extends Base
{
function Derived2Fun()
{
echo "Derived2Fun() called<br>";
}
}

$Obj1 = new Derived1();


$Obj2 = new Derived2();

$Obj1->BaseFun();
$Obj1->Derived1Fun();

echo "<br>";

$Obj2->BaseFun();
$Obj2->Derived2Fun();
Output
BaseFun() called
?> Derived1Fun() called

BaseFun() called
Derived2Fun() called
Multiple Inheritance(Using interface)
 Multiple inheritance is a type of inheritance in which one class can inherit the
properties from more than one parent class.

<?php
//PHP program to implement multiple-inheritance using the
interface.
class Base
{
public function Fun1()
{
printf("Fun1() called<br>");
}
}

interface Inf
{
public function Fun2();
}
class Derived extends Base implements Inf
{
function Fun2()
{
printf("Fun2() called<br>");
}

function Fun3()
{
printf("Fun3() called<br>");
}
}

$obj = new Derived();

$obj->Fun1();
$obj->Fun2();
$obj->Fun3();
Output
?> Fun1() called
Fun2() called
Fun3() called
Create a class as “Percentage” with two properties length & width. Calculate area of
rectangle for two objects.

You might also like