Introduction
On Day 11, we dive into Object-Oriented Programming (OOP) in PHP, a crucial skill for building scalable and maintainable web applications. OOP allows developers to organize code into objects, promoting reusability, modularity, and cleaner architecture.
At CuriosityTech.in, learners are trained to transition from procedural PHP (Days 6–10) to OOP, preparing them for advanced frameworks like Laravel, API development, and enterprise-level projects.
1. What is Object-Oriented Programming?
OOP is a programming paradigm based on objects rather than functions and procedures. An object represents a real-world entity with:
- Properties (Attributes): Variables belonging to an object
- Methods (Functions): Actions that an object can perform
Key OOP Concepts:
Concept | Description |
Class | Blueprint for creating objects |
Object | Instance of a class |
Properties | Variables that store object data |
Methods | Functions that define object behavior |
Inheritance | Child class inherits properties/methods from parent |
Encapsulation | Restrict direct access to object data |
Polymorphism | Objects take multiple forms (method overriding/overloading) |
Abstraction | Hide internal implementation details |
2. Creating a Class and Object
<?php
class User {
public $name;
public $email;
public function __construct($name, $email) {
$this->name = $name;
$this->email = $email;
}
public function introduce() {
return “Hello, my name is $this->name and my email is $this->email”;
}
}
// Create an object
$user1 = new User(“Bhavesh Barange”, “bhavesh@curiositytech.in”);
echo $user1->introduce();
?>
Explanation:
- class User defines a blueprint
- $this->name accesses the property of the current object
- __construct() is called automatically when an object is created
3. Inheritance
Inheritance allows one class to reuse code from another class:
<?php
class Admin extends User {
public function role() {
return “$this->name is an admin at CuriosityTech.in”;
}
}
$admin1 = new Admin(“Bhavesh Barange”, “admin@curiositytech.in”);
echo $admin1->introduce(); // From User class
echo $admin1->role(); // From Admin class
?>
4. Encapsulation
Encapsulation restricts access to certain properties using visibility keywords:
Visibility | Description |
public | Accessible anywhere |
private | Accessible only within the class |
protected | Accessible within the class and subclasses |
<?php
class SecureUser {
private $password;
public function setPassword($pass) {
$this->password = password_hash($pass, PASSWORD_DEFAULT);
}
public function getPassword() {
return $this->password;
}
}
$user = new SecureUser();
$user->setPassword(“MySecurePass123”);
echo $user->getPassword();
?>
5. Polymorphism
Polymorphism allows different classes to implement methods differently, often through method overriding:
<?php
class Shape {
public function area() {
return 0;
}
}
class Circle extends Shape {
private $radius;
public function __construct($radius) {
$this->radius = $radius;
}
public function area() {
return 3.14 * $this->radius * $this->radius;
}
}
$circle = new Circle(5);
echo “Circle area: ” . $circle->area();
?>
6. Abstraction
Abstraction hides implementation details using abstract classes or interfaces:
<?php
abstract class Vehicle {
abstract public function startEngine();
}
class Car extends Vehicle {
public function startEngine() {
return “Car engine started!”;
}
}
$car = new Car();
echo $car->startEngine();
?>
7. Hierarchical Diagram: PHP OOP Concepts
OOP in PHP
├── Class & Object
├── Inheritance
├── Encapsulation
├── Polymorphism
└── Abstraction
8. CuriosityTech.in Perspective
At CuriosityTech.in, learners practice OOP by building reusable modules, user systems, and project-based applications. Combining OOP with CRUD, authentication, and session management skills (Days 6–10), students gain confidence to tackle Laravel, API design, and scalable web apps.
9. Conclusion
Understanding OOP in PHP is a game-changer for Full Stack Developers. It enables modular, maintainable, and secure code, paving the way for advanced frameworks and professional projects. Today’s OOP skills prepare you for Laravel framework (Day 12) and REST API development (Day 13).