0% found this document useful (0 votes)
17 views54 pages

Itelec3c Module 5 - PHP Oop

Uploaded by

Josh Penascosas
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)
17 views54 pages

Itelec3c Module 5 - PHP Oop

Uploaded by

Josh Penascosas
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/ 54

COLLEGE OF INFORMATION AND COMPUTING SCIENCES

ITELEC3C: Web Development using PHP

OOP Concepts in PHP:


Classes and Objects
Module 5 | Part 1

Engr. Errol John M. Antonio


Course Facilitator
Information Technology Department
University of Santo Tomas
[email protected]

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Learning Objectives
At the end of this module, the learner will be able to:
▪ understand the concepts of OOP in PHP;
▪ implement classes, objects, constructors, destructors,
getters, and setters in PHP; and
▪ understand the concept and implementation of inheritance.

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Classes and Objects


▪ A class is a group (blueprint) of similar objects.
▪ An object is an instance of a class.

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Defining a Class
To define a class, you specify the class keyword
followed by a name like this:

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Defining a Class
For example, the following defines a new class called
BankAccount:

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Instantiating an Object
From the BankAccount class, you can create a new
bank account object by using the new keyword like this:

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Adding Properties to a Class


To add properties to the BankAccount class, you
place variables inside it.

How many properties does the class has?


[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Adding Properties to a Class


The public keyword determines the visibility of a
property. In this case, you can access the property from
the outside of the class.
To access a property, you use the object operator (->):

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Adding Properties to a Class

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Adding Properties to a Class


Aside from public, what other access modifier can
we use?

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Adding Methods to a Class

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Adding Methods to a Class


Method Access Specifiers: public, private, and
protected.

By default, methods are ________.

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Adding Methods to a Class

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Calling a Method
To call a method, you also use the object operator (->)
as follows:

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Calling a Method

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Now, let’s create another method, withdraw()

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Question:
What happens when the balance is less than the
withdrawal amount?

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Chaining Methods

This technique is called method chaining. To form the method


chaining, the deposit() method needs to return a
BankAccount object,
[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Chaining Methods

The deposit() returns the $this which is the current


object of the BankAccount class. Therefore, you can call
any public method of the BankAccount class.
[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Chaining Methods
The following example calls the deposit() method
first and then the withdraw() method in a single
statement:

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Summary:
▪ Objects have states and behaviors.
▪ A class is a blueprint for creating objects.
▪ Properties represent the object’s state, and methods
represent the object’s behavior. Properties and
methods have visibility.
▪ Use the new keyword to create an object from a
class.
▪ The $this variable references the current object of
the class.
[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

ITELEC3C: Web Development using PHP

OOP Concepts in PHP:


Access Modifiers
Module 5 | Part 2

Engr. Errol John M. Antonio


Course Facilitator
Information Technology Department
University of Santo Tomas
[email protected]

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

PHP Access Modifiers


PHP has three access modifiers: public, private,
and protected
The public access modifier allows you to access
properties and methods from both inside and
outside of the class.
The private access modifier prevents you from
accessing properties and methods from the outside
of the class.

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

public Access Modifier


When you place the public keyword in front of a property or a method,
the property or method becomes public. It means that you can access
the property and method from both inside and outside of the class.

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

public Access Modifier

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

private Access Modifier


To prevent access to properties and methods from outside of
the class, you use the private access modifier.

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

private Access Modifier


What will happen when you attempted to access a
private property?

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

How to Access private Property?


To manipulate the value of a private property, you need to
define a public method and use the method to manage a
private property.
Typically, you need to define two kinds of public methods to
manage a private property:
▪ A getter returns the value of the private property.
▪ A setter sets a new value for the private property.

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

How to Access private Property?


By convention, the getter and setter
are prefixed with get and set.

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Why Use private Property?


Isn’t it easier and faster to use public access modifier
than private?

By using the private property, you can prevent direct


access to the property from the outside of the class.

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Summary
▪ Use the public access modifier to allow access to
properties and methods from both inside and
outside of the class.
▪ Use the private access modifier to prevent access
from the outside of the class.
▪ Do use private properties with a pair of public
getter/setter methods.

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

ITELEC3C: Web Development using PHP

BIO BREAK
Engr. Errol John M. Antonio
Course Facilitator
Information Technology Department
University of Santo Tomas
[email protected]

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

ITELEC3C: Web Development using PHP

OOP Concepts in PHP:


Constructors
Module 5 | Part 3

Engr. Errol John M. Antonio


Course Facilitator
Information Technology Department
University of Santo Tomas
[email protected]

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Constructors
PHP allows you to declare a constructor method for a
class with the name __construct()

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Constructors
When you create an instance of the class, PHP
automatically calls the constructor method.

Typically, constructors are used to initialize the


properties of the object.

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Constructors
The following example defines a constructor for the BankAccount class.
The constructor initializes the $accountNumber and $balance properties:

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Construction Promotion
In practice, you often need to assign the constructor
arguments to corresponding properties. It’s kind of
redundant.

To improve this, PHP 8.0 introduced the new concept


called constructor promotion that promotes the
constructor’s arguments to properties.

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Construction Promotion
When a constructor parameter includes an access
modifier (public, private, or protected) PHP will
treat it as both a constructor’s argument and an
object’s property. And it assigns the constructor
argument to the property.

Sometimes, you don’t want to promote constructor


arguments, you can remove the access modifier.

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Construction Promotion

Instead of implementing this way

Use this approach.

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Summary:
▪ PHP constructor is a special method that is called
automatically when an object is created.
▪ Do use constructor promotion as much as possible
to make the code shorter.

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

ITELEC3C: Web Development using PHP

OOP Concepts in PHP:


Inheritance
Module 5 | Part 4

Engr. Errol John M. Antonio


Course Facilitator
Information Technology Department
University of Santo Tomas
[email protected]

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Inheritance
▪ Inheritance allows a class to reuse the code from another class
without duplicating it.
▪ In inheritance, you have a parent class with properties and
methods, and a child class can use the code from the parent
class.
▪ Inheritance allows you to write the code in the parent class and
use it in both parent and child classes.
▪ The parent class is also called a base class or super class; and
the child class is also known as a derived class or a subclass.
▪ To define a class inherits from another class, you use the
extends keyword
[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Inheritance
Suppose we have the bankAccount:

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Question:
Can SavingAccount use all properties and methods
from the BankAccount class?

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Inheritance
The following creates a new instance of the SavingAccount
class, calls the deposit() method and shows the balance:

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Question
(True/False) A child class can reuse properties and
methods from the parent class. But the parent class
cannot use properties and methods from the child
class.

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Adding Properties and Methods to Child Class


A child class can have its own properties and methods. In the following
example, we add the $interestRate property and setInterestRate()
method to the SavingAccount class:

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Calling Methods From the Parent Class


To call a method from the parent class, you use the $this keyword.

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Summary
▪ Inheritance allows a class to reuse the code of another class
without duplicating it.
▪ Use the extends keyword to define one class that inherits
from another class.
▪ A class that inherits another class is called a subclass, a
child class, or a derived class. The class from which the
subclass inherits is a parent class, a superclass, or a base
class.
▪ A subclass can have its own properties and methods.
▪ Use $this keyword to call the methods of the parent class
from methods in the child class.
[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

– End of Module –

Download PDF

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES

Reference
SimpliLearn. (n.d.). ReactJS Tutorial: A Step-by-Step Guide To Learn
React. https://www.simplilearn.com/tutorials/reactjs-tutorial

ReactJS Documentation: https://reactjs.org/docs/getting-started.html

[email protected]

You might also like