Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects" containing data (properties) and code (methods). OOP organizes software design around data structures rather than standalone procedural functions.
| Feature | Procedural Programming | Object-Oriented Programming (OOP) |
|---|---|---|
| Structure | Functions operating on loose data | Classes encapsulating data & behavior together |
| Data Security | Data exposed globally or passed across functions | Encapsulated via access modifiers (private, protected) |
| Code Reuse | Function calls & files | Inheritance, Polymorphism, Traits, & Interfaces |
| Maintainability | Harder to scale as codebase grows | Modular, testable, and maintainable enterprise architecture |
flowchart TD
A["4 Pillars of OOP"] --> B["1. Encapsulation"]
A --> C["2. Abstraction"]
A --> D["3. Inheritance"]
A --> E["4. Polymorphism"]
B --> B1["Bundling data & restricting direct property access"]
C --> C1["Hiding internal complexity behind simple interfaces"]
D --> D1["Reusing parent class features in child classes"]
E --> E1["Objects of different classes sharing identical method signatures"]
<?php
declare(strict_types=1);
// Encapsulated Class Definition
class UserAccount
{
// Encapsulated Private Properties
private string $email;
private bool $isActive = true;
public function __construct(string $email)
{
$this->setEmail($email);
}
// Getter Method
public function getEmail(): string
{
return $this->email;
}
// Setter Method with Validation
public function setEmail(string $email): void
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new InvalidArgumentException("Invalid email format.");
}
$this->email = $email;
}
}
// Instantiating Object
$user = new UserAccount("[email protected]");
echo "Encapsulated User Email: " . $user->getEmail() . "
";
private or protected and expose public getter/setter methods.Name the 4 core pillars of Object-Oriented Programming! (Encapsulation, Abstraction, Inheritance, Polymorphism)
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.