PHP OOP Part 1
PHP OOP Part 1
Salman MD Sultan
European IT Solutions Institute
Contents
Motivation
What is OOP
Constructor & Destructor
Access Modifiers
Inheritance
Final
Motivation
1) OOP provides a clear modular structure for programs
2) OOP makes it easy to maintain and modify existing code
3) OOP provides a good framework for code libraries
where supplied software components can be easily adapted
and modified by the programmer.
4)Code Reusability
5)It is suitable for real word problems and real world works
Procedural PHP VS OOP
3 Tasks PHP DOES:
Query the database and connect to it
We handle data submitte by users
We show db to data to users
What are objects in OOP
Anything in the world can be a object
For example, in a e-commerce website,
Product is a object
A Customer is object
A cart is an object
For example, in a Education Management System,
Student is a object
Department is a object
Teacher is a object
Classes
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 (__)!
Destructor
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 (__)!
Access Modifies
Access Modifies
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.
Inheritance (Protected)
Override Inherit methods
Delete Objects
Delete Objects
PHP – Class Constants
Constants cannot be changed once it is declared.
Class constants can be useful if you need to define some constant data within a class.
A class constant is declared inside a class with the const keyword.
Class constants are case-sensitive. However, it is recommended to name the constants in
all uppercase letters.
We can access a constant from outside the class by using the class name followed by the
scope resolution operator (::) followed by the constant name, like here:
Output