Lesson 5
Objected
Oriented PHP
1
Lesson Learning
Outcomes
• At the end of this Topic, the learner
should be able to:
i. Write a OBJECT ORIENTED PHP
programs using classes and objects
2
Introduction
OOP stands for Object-Oriented
Programming which is faster and easier
to execute, provides a cleaner structure
for the programs and creates programs
that are reusable makes the code easier
to maintain, modify and debug.
The opposite is Procedural
programming which is about writing
procedures or functions that perform
operations on the data
3
Object Oriented Concepts
Class − This is a programmer-defined data type, which includes
local functions as well as local data. You can think of a class as a
template for making many instances of the same kind (or class) of
object.
Object − An individual instance of the data structure defined by a
class. You define a class once and then make many objects that
belong to it. Objects are also known as instance.
Member Variable − These are the variables defined inside a class.
This data will be invisible to the outside of the class and can be
accessed via member functions. These variables are called attribute
of the object once an object is created.
Member function − These are the function defined inside a class
and are used to access object data.
Inheritance − When a class is defined by inheriting existing
function of a parent class then it is called inheritance. Here child
class will inherit all or few member functions and variables of a
parent class.
4
Object Oriented Concepts(cont.)
Parent class − A class that is inherited from by another class. This is also called a
base class or super class.
Child Class − A class that inherits from another class. This is also called a subclass
or derived class.
Polymorphism − This is an object oriented concept where same function can be
used for different purposes. For example function name will remain same but it take
different number of arguments and can do different task.
Overloading − a type of polymorphism in which some or all of operators have
different implementations depending on the types of their arguments. Similarly
functions can also be overloaded with different implementation.
Data Abstraction − Any representation of data in which the implementation details
are hidden (abstracted).
Encapsulation − refers to a concept where we encapsulate all the data and
member functions together to form an object.
Constructor − refers to a special type of function which will be called automatically
whenever there is an object formation from a class.
Destructor − refers to a special type of function which will be called automatically
whenever an object is deleted or goes out of scope.
5
Defining a Class
<?php
class Fruit {
// Properties
public $name;
public $color;
// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
?>
6
Defining an Object
<?php
class Fruit {
// Properties
public $name;
public $color;
// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
$apple = new Fruit();
$banana = new Fruit();
$apple->set_name('Apple');
$banana->set_name('Banana');
echo $apple->get_name();
echo "<br>";
echo $banana->get_name();
?>
7
Defining a Constructor
<?php
class Fruit {
public $name;
public $color;
function _ _construct($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
$apple = new Fruit("Apple");
echo $apple->get_name();
?>
8
Defining a Destructor
<?php
class Fruit {
public $name;
public $color;
function _ _construct($name) {
$this->name = $name;
}
function _ _destruct() {
echo "The fruit is {$this->name}.";
}
}
$apple = new Fruit("Apple");
?>
9
PHP - Access Modifiers
<?php
class Fruit {
public $name;
protected $color;
private $weight;
}
$mango = new Fruit();
$mango->name = 'Mango'; // OK
$mango->color = 'Yellow'; // ERROR
$mango->weight = '300'; // ERROR
?>
10
The end
Thank you
02/21/2025 11