In this tutorial, we will be learning about object-oriented programming and how it is implemented in PHP.
What is Object-Oriented PHP Programming?
First thing first, what exactly is object-oriented programming?
Object-oriented development is a way of writing programs that use classes, objects, relationships, and properties to make writing your program easier.
You don't have to use it, but it makes your code better organized and is particularly helpful when you want to reuse code.
The two most basic concepts in object-oriented programming are classes and objects.
To become an excellent programmer you must clearly understand what they are.
Objects
So in the context of object-oriented software, an object is a representation of something: this something can be almost any item or concept. For example, you can represent a physical object, such as a car or house or a customer, or you can represent a conceptual thing that only exists in programming such as a file.
Objects come with two types of things: attributes and operations.
Attributes are properties or variables that relate to a particular object.
Operations are methods, actions, or functions that an object can use (often to modify something).
The term "attribute" can be used interchangeably with property while the term "operation" can be used interchangeably with method.
Encapsulation One of the big advantages of OOP is it supports and encourages encapsulation; encapsulation is a fancy way of saying "data hiding".
This means, in order to access data to your object, you must use one of the object's method.
So why might you want to use encapsulation? The benefit of encapsulation is you can easily change the details on how an object is implemented without making any changes to the interface of the object.
Bringing Data To Life
In object-oriented software, an object is a unique collection of stored data with operations that work on the data.
Their main purpose is to bring our data to life.
If you have a table of students and want to make some changes to the table, you can bring each row of student data out and create an object out of them.
And with a property constructed class, you can make changes to each row in your table.
Classes Beside object, another very important concept in object-oriented programming is classes. Objects that have similar properties and operations can be grouped together as classes.
Classes are used to represent a set of objects that have certain properties and methods in common.
While the value of the property properties and methods may differ, the properties and methods names are the same.
For example, a yellow button and a green button are both objects of the button class.
All buttons will have a property of colour, the colour may be different. But you cannot have a colourless button.
For example, you can think of a car class.
Cars vary in many ways including in brand, colour and year manufactured however, they also all have some properties which are the same such as having four wheels.
Also, all cars must have the operation of drive.
While not every car has a colour of blue or not all cars are Hondas, but you never see a one-wheeled car without the operation of drive()
.