Lesson-7-PHP-OOP
Lesson-7-PHP-OOP
PHP-OOP
3/11/2023 1
Introduction
• OOP stands for Object-Oriented Programming.
• Procedural programming is about writing procedures or
functions that perform operations on the data, while
object-oriented programming is about creating objects
that contain both data and functions.
3/11/2023 2
Advantages of OOP
• Object-oriented programming has several advantages
over procedural programming:
3/11/2023 3
PHP Classes and Objects
• A class is a template for objects, and an object is an
instance of class.
• Let's assume we have a class named Fruit. A Fruit can
have properties like name, color, weight, etc.
• We can define variables like $name, $color, and $weight
to hold the values of these properties.
3/11/2023 5
Define a Class
• A class is defined by using the class keyword, followed by
the name of the class and a pair of curly braces ({}).
• All its properties and methods go inside the braces:
Syntax
• <?php
• class Fruit {
• // code goes here...
•}
• ?>
3/11/2023 6
Define a Class(cont.)
• Below we declare a class named Fruit consisting
of two properties ($name and $color) and two
methods set_name() and get_name() for setting
and getting the $name property:
3/11/2023 7
Define a Class(cont.)
• <?php
class Fruit {
// Properties
public $name;
public $color;
// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
?>
3/11/2023 8
Define Objects
• 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.
3/11/2023 9
Define Objects(cont.)
<?php
class Fruit {
// Properties
public $name;
public $color; $apple = new Fruit();
$banana = new Fruit();
// Methods $apple->set_name('Apple');
function set_name($name) { $banana->set_name('Banana');
$this->name = $name;
} echo $apple->get_name();
function get_name() { echo "<br>";
return $this->name; echo $banana->get_name();
} ?>
}
3/11/2023 10
Define Objects(cont.)
In the example below, we add two more methods to class Fruit,
for setting and getting the $color property:
2
So, where can we
change the value of the
$name property? There
are two ways:
3/11/2023 13
PHP instanceof
• Example
• <?php
• $apple = new Fruit();
• var_dump($apple instanceof Fruit);
• ?>
3/11/2023 14
PHP OOP - Constructor
• A constructor allows you to initialize an object's
properties upon creation of the object.
3/11/2023 15
PHP OOP Constructor(cont.)
• <?php
• class Fruit {
• public $name;
• public $color;
• function __construct($name) {
• $this->name = $name;
• }
• function get_name() {
• return $this->name;
• }
• }
3/11/2023 17
PHP - Access Modifiers
• Properties and methods can have access modifiers
which control where they can be accessed.
3/11/2023 18
PHP - Access
Modifiers(cont.)
• In the following example we have added three
different access modifiers to three properties
(name, color, and weight).
3/11/2023 19
PHP - Access
Modifiers(cont.)
3/11/2023 20
PHP - Access
Modifiers(cont.)
3/11/2023 21
PHP - Access
Modifiers(cont.)
3/11/2023 22
PHP OOP Inheritance
3/11/2023 23
PHP OOP
Inheritance(cont.)
3/11/2023 24
THE END
Thank you!
3/11/2023 25