The Object-Oriented Programming (OOPs) concept is one of the most important learning sections for anyone who wants to learn a back-end programming language. PHP supports OOPs concepts for complex coding. The Object-Oriented Programming (OOPs) concept is one of the major features of PHP. Like any other programming language, PHP provides OOPs concepts to develop advanced applications.
Index
Object-Oriented Programming (OOPs) in PHP
Object-oriented programming is a programming model that is used to represent real-world entities using classes and objects. It is the modern approach to solving real-world problems. In this method, the user focuses on an object while programming, and not on the procedure.
Class in OOP PHP
- The class is the blueprint of an object.
- It contains all the information about an object.
- It is a combination of variables and methods.
- We can define a class using the ‘class’ keyword.
Syntaxย
class c_name{ // variables of class // methods of class }
Here, in the first step, we declare different variables of class, and then we declare and define the methods of a class.
For example, we want to create a class for the vehicle; we can do this as follows:
<?php class Vehicle{ // variables public $vtype; public $color; public $price; public $speed; // methods public getdata($data){ // code } public setdata(){ // code } } ?>
Explanation
- First, we declare the variables of class, such as vtype, color, price, and speed.
- Next, we declare the methods: the getdata() and the setdata(), and inside these methods, we define the methods.
Here the class is the basic structure for all the objects of the class vehicle. We can create different objects according to the requirement.
All the objects have the same variables and methods.
The class gives us the necessary information about an object by providing an answer for two questions as below:
- How an object will behave?
- What an object will contain?
So the class is the combination of the variables and procedures. Procedure uses variables to define the activity of an object.
In this way, we can define all the real-world entities using classes such as a person, fruit, flower, animal, etc.
Object in OOP PHP
- The object is an instance of the class.
- It is a real-world entity.
- We can create an object using the new keyword in PHP.
Syntax
class Classname{ // variables and methods } $objname = new Classname();
Here $objename is the name of an object, and Classname is the name of the class for which we want to create an object. We can create multiple objects of a class.
For example, we have a class vehicle as explained above, and we want to create an object of the class we can do this as follows:
<?php class Vehicle{ // variables public $vtype; public $color; public $price; public $speed; // methods public getdata($data){ // code } public setdata(){ // code } } $bike = new Vehicle(); ?>
Explanation
- First, we declare and define the class vehicle.
- Next, we create an object of the class named $bike.
We can create as many objects as we want. There is no restriction on the amount of an object. All the objects have the same properties and methods defined in class but have different values.
Example of class and object:
Suppose we have a class vehicle having the variables vehicle type, color, and amount, and methods for getting data and setting values for them.
<!DOCTYPE html> <html> <body> <?php class Vehicle { // Properties public $vtype; public $amount; public $color; // Methods function set_vtype($vtype) { $this->vtype = $vtype; } function get_vtype() { return $this->vtype; } function set_amount($amount) { $this->amount = $amount; } function get_amount() { return $this->amount; } function set_color($color) { $this->color = $color; } function get_color() { return $this->color; } } $bike = new Vehicle(); $bike->set_vtype('Gearless'); $bike->set_amount('50000'); $bike->set_color('Red'); echo "Vehicle type: " . $bike->get_vtype(); echo "<br>"; echo "Amount: " . $bike->get_amount(); echo "<br>"; echo "Color: " . $bike->get_color(); ?> </body> </html>
Output
Vehicle type: Gearless
Amount: 50000
Color: Red
This is all about classes and objects in OOP using PHP. Object-oriented programming also has certain additional concepts which are essential to understand for using OOP efficiently.
- Encapsulation
- Inheritance
- Polymorphism
Encapsulation
The binding of the properties to an object is called encapsulation.
It is also used for wrapping some data into a single unit.
It is used for data hiding. It is mainly used for providing data security and reducing complexity.
It uses the GET and the SET methods in PHP.
Inheritance
It is used to develop the relationship between the classes. It is used in defining the parent and child relationship. Here the child can use all the properties of the parent.
It uses the โextendโ keyword.
For example, we have a class Person, containing the name and age properties. We also have a class student and professor. Here we can inherit the properties of person class with student or professor class. It develops the parent-child relationship between them. It is mainly used in the reusability of the code multiple times in a program.
Polymorphism
It is the concept of having one form with different implementation methods. Here methods can be differentiated according to the type and numbers of the variables.
It uses the ‘implements’ keyword.
Conclusion
This article gave an overview of all the OOPs concepts in PHP. We should have complete knowledge of how to use class and objects while coding for real-world problems. We should also consider inheritance and polymorphism to make the code reusable and more efficient.
We hope you have gained a complete understanding of the OOPs concepts in PHP programming. In case you want to learn about the other concepts, refer to our PHP Tutorial, PHP MySQL, PHP Form and PHP OOP sections. Happy Learning!