Lesson 7
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:
• OOP is faster and easier to execute
• OOP provides a clear structure for the programs
• OOP helps to keep the PHP code DRY "Don't Repeat Yourself",
and makes the code easier to maintain, modify and debug
• OOP makes it possible to create full reusable applications with
less code and shorter development time
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.
• When the individual objects (apple, banana, etc.) are
created, they inherit all the properties and behaviors
from the class, but each object will have different values
for the properties.
3/11/2023 4
PHP Classes and Objects
• When the individual objects (apple, banana, etc.) are
created, they inherit all the properties and behaviors
from the class, but each object will have different values
for the 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.
• Objects of a class are created using the new
keyword.
• In the example below, $apple and $banana are
instances of the class Fruit:
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:
<?php $apple = new Fruit();
class Fruit {
$apple->set_name('Apple');
// Properties
public $name; $apple->set_color('Red');
public $color; echo "Name: " . $apple->get_name();
echo "<br>";
// Methods echo "Color: " . $apple->get_color();
function set_name($name) { ?>
$this->name = $name;
}
function get_name() {
return $this->name;
}
function set_color($color) { Output
$this->color = $color; Name: Apple
}
function get_color() {
Color: Red
[see classes-objects-1.php]
return $this->color;
}
}
3/11/2023 11
PHP $this Keyword
The $this keyword refers to the current object, and is only
available inside methods.
Look at the following example:
1
3
2
So, where can we
change the value of the
$name property? There
are two ways:
1. Inside the class (by
adding a set_name()
method and use $this):
3/11/2023 12
PHP $this
Keyword(cont.)
2. Outside the class (by directly changing the property value):
Look at the following example:
3/11/2023 13
PHP instanceof
• You can use the instanceof keyword to
check if an object belongs to a specific
class:
• 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.
• If you create a __construct() function, PHP will
automatically call this function when you create an
object from a class.
• Notice that the construct function starts with two
underscores (_ _)!
• We see in the example below, that using a
constructor saves us from calling the set_name()
method which reduces the amount of code:
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;
• }
• }
• $apple = new Fruit("Apple");
• echo $apple->get_name();
• ?>
3/11/2023 16
PHP - The __destruct
Function
• A destructor is called when the object is destructed
or the script is stopped or exited.
• If you create a __destruct() function, PHP will
automatically call this function at the end of the
script.
• Notice that the destruct function starts with two
underscores (__)!
• The example below has a __construct() function
that is automatically called when you create an
object from a class, and a __destruct() function
that is automatically called at the end of the script:
3/11/2023 17
PHP - Access Modifiers
• Properties and methods can have access modifiers
which control where they can be accessed.
• There are three access modifiers:
• public - the property or method can be
accessed from everywhere. This is default
• protected - the property or method can be
accessed within the class and by classes derived
from that class
• private - the property or method can ONLY be
accessed within the class
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).
• Here, if you try to set the name property it will
work fine (because the name property is public, and
can be accessed from everywhere).
• However, if you try to set the color or weight
property it will result in a fatal error (because the
color and weight property are protected and
private):
3/11/2023 19
PHP - Access
Modifiers(cont.)
3/11/2023 20
PHP - Access
Modifiers(cont.)
• In the next example we have added access
modifiers to two functions.
• Here, if you try to call the set_color() or the
set_weight() function it will result in a fatal error
(because the two functions are considered
protected and private), even if all the properties are
public:
3/11/2023 21
PHP - Access
Modifiers(cont.)
3/11/2023 22
PHP OOP Inheritance
• Inheritance in OOP = When a class derives from
another 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.
• An inherited class is defined by using the extends
keyword.
3/11/2023 23
PHP OOP
Inheritance(cont.)
• The Strawberry class is inherited from the Fruit
class.
• This means that the Strawberry class can use the
public $name and $color properties as well as the
public __construct() and intro() methods from the
Fruit class because of inheritance.
• The Strawberry class also has its own method:
message().
3/11/2023 24
THE END
Thank you!
3/11/2023 25