Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects that combine data and methods to work with that data. The core principles of OOP help developers create flexible, resilient, and easily extendable code. Let’s look at the main principles of OOP and their application in PHP.
1. Encapsulation
Encapsulation involves combining data and methods that work with that data into a single entity—an object. This allows the internal implementation of the object to be hidden and provides access only to the necessary methods.
Example:
class User { private $name; private $email; public function __construct($name, $email) { $this->name = $name; $this->email = $email; } public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } public function getEmail() { return $this->email; } public function setEmail($email) { $this->email = $email; } }
In this example, the data $name
and $email
are private and can only be accessed through the methods getName
, setName
, getEmail
, and setEmail
.
2. Principle of Inheritance
Inheritance allows creating new classes based on existing ones by inheriting their properties and methods. This helps to avoid code duplication and improves its organization.
Example:
class Employee extends User { private $position; public function __construct($name, $email, $position) { parent::__construct($name, $email); $this->position = $position; } public function getPosition() { return $this->position; } public function setPosition($position) { $this->position = $position; } }
The class Employee inherits all the properties and methods of the User class and adds a new property, $position.
3. Principle of Polymorphism
Polymorphism allows the use of objects from different classes through a single interface, without knowing their specific type. This simplifies code extensibility and maintainability.
Example:
interface Payable { public function calculateSalary(); } class FullTimeEmployee implements Payable { public function calculateSalary() { return 5000; } } class PartTimeEmployee implements Payable { public function calculateSalary() { return 2000; } } function processSalary(Payable $employee) { return $employee->calculateSalary(); }
The function processSalary
can work with objects of any class that implements the Payable
interface, whether it’s FullTimeEmployee
or PartTimeEmployee
.
4. Principle of Abstraction
Abstraction allows the creation of simplified models of objects by focusing only on significant characteristics and hiding unnecessary details. Abstraction helps to concentrate on the essential aspects of the problem, making its solution easier.
Example:
abstract class Vehicle { protected $speed; abstract public function move(); } class Car extends Vehicle { public function move() { return "Car is moving at speed " . $this->speed; } }
The class Vehicle is abstract and cannot be instantiated directly, but it provides a general structure for all vehicles, such as Car.
Conclusion
The principles of OOP—encapsulation, inheritance, polymorphism, and abstraction—form the foundation for creating sustainable and flexible code. They help structure code, making it more understandable and maintainable. By following these principles, you can develop applications that easily adapt to changes and expansions.