Itelec3c Module 5 - PHP Oop
Itelec3c Module 5 - PHP Oop
[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
[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
[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
[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
[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
Chaining Methods
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
[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
[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
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
[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.
[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.
[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.
[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Construction Promotion
[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
[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
[email protected]
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
[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