Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Object-Oriented Programming (OOP) is a powerful programming paradigm that organizes code into reusable, modular structures called "objects," each representing a specific aspect of the application. In PHP, adopting OOP principles makes your code more organized, scalable, and easier to maintain, especially for complex projects. Whether you’re building a web application, API, or complex system, understanding and using OOP in PHP will help you structure your code logically and enable you to take advantage of features like inheritance, encapsulation, and polymorphism.
What are OOP concepts in PHP?
Object-Oriented Programming (OOP) in PHP is built around four key concepts:
Encapsulation: Bundles data and methods into a single unit (class) and restricts access to some components, enhancing data protection.
Inheritance: Allows a class to inherit properties and methods from another class, promoting code reuse and creating a hierarchical relationship.
Polymorphism: Enables methods to perform differently based on the object that is calling them, allowing for dynamic method resolution.
Abstraction: Hides complex implementation details and exposes only the essential features, simplifying interaction with objects.
These principles foster code organization, scalability, and maintainability in PHP applications.
Benefits of OOP in PHP
The benefits of using OOP in PHP are discussed below
Allows us to write the proper code in a structured way.
Code once written can be reused at multiple places.
Data that remains hidden cannot be accessed by anyone unless made public.
Visualizing in the real world is easy.
Helps to keep the PHP code DRY(Don't Repeat Yourself), and also makes the code easier to maintain, debug and modify
Classes and Objects in OOP
Wrapper that encapsulates data and related functions is termed as a class. For example, a student class would include roll number and name data; a teacher class would include name, id, and subjects. Simply put, class is the blueprint of Objects.
Now take, for example, “John” is a Student, and so data and functions operating on him will come from the Student class. “John” is the Object here, one instance of Student class and will hold property values as described in the class definition.
The below diagram illustrates classes and objects
Classes
Objects
City
Jaisalmer
Jodhpur
Patna
Country
India
Nepal
USA
Syntax
To define a class in PHP, use the “class” tag with the name of the class. Refer below for example
class City{
// code...
}
Now we will define the blueprint with data and required functions as below
class City{
//properties of the class
public $state;
//methods that operate on property
function set_state_name($state){
$this->state=$state;
}
function get_state_name($state){
return $this->state;
}
}
Creating an object out of a class is done with the help of the “new” keyword. Each object instance will have its property values. Refer below to create an instance of the city named “Jaisalmer”.
$jaisalmer=new City();
More Example
PHP
PHP
<?php class Car { public $manufacturer; public $color;
function set_manufactuer($name) { $this->manufacturer = $name; } function get_manufacturer() { return $this->manufacturer; } } $tigor=new Car(); $tigor->set_manufactuer('Tigor'); echo $tigor->get_manufacturer(); ?>
You can also try this code with Online PHP Compiler
Class Definition: The Car class is defined with three properties: make, model, and color. These properties will hold the specific details of a car.
Constructor: The __construct method is a special method that initializes the properties when a new object is created. It assigns the values passed as arguments to the object's properties using $this, which refers to the current object.
Method: The displayInfo method is defined to return a string containing details about the car. It accesses the object's properties using $this->propertyName.
Creating an Object: The line $myCar = new Car("Toyota", "Corolla", "blue"); creates a new object called $myCar from the Car class, initializing it with specific values for make, model, and color.
Accessing Properties and Methods: Finally, echo $myCar->displayInfo(); calls the displayInfo method on the $myCar object, which outputs the car's details.
Frequently Asked Question
What are the 4 concepts of OOP?
The four concepts of OOP are encapsulation, inheritance, polymorphism, and abstraction, which promote modular, reusable, and organized code.
How to create an OOP in PHP?
To create OOP in PHP, define classes using the class keyword, create objects with the new keyword, and use methods and properties.
Can PHP use OOP?
Yes, PHP fully supports OOP, allowing developers to implement classes, objects, and all core OOP principles for better code organization and reusability.
Conclusion
Congratulations on getting through this article. We got introduced to object oriented programming i.e., OOP in PHP. We understood how it is different from procedural programming. We saw how to define classes and instance objects with illustrations and examples.