PHP OOP (Object-Oriented Programming)
PHP is not only for writing simple
scripts — it also supports Object-Oriented Programming (OOP). OOP helps
you structure code into classes and objects, making applications
more organized, reusable, and easier to maintain.
✅
What is OOP?
In simple terms:
- Class
→ A blueprint (like a design or template).
- Object
→ A real thing created from the class (like an actual car made from the
blueprint).
OOP groups related properties (data)
and methods (functions) together, instead of writing scattered functions.
🔑 Core OOP Concepts in PHP
1.
Class and Object
A class is a blueprint, and
an object is an instance of that class.
<?php
//
Define a class
class
Car {
public $brand;
public $color;
public function drive() {
echo "The car is driving!";
}
}
//
Create an object
$myCar
= new Car();
$myCar->brand
= "Toyota";
$myCar->color
= "Red";
echo
$myCar->brand; // Output: Toyota
$myCar->drive(); //
Output: The car is driving!
?>
2.
Constructor (__construct)
Constructors automatically run when
an object is created.
<?php
class
Car {
public $brand;
public function __construct($brand) {
$this->brand = $brand;
}
}
$car1
= new Car("Honda");
echo
$car1->brand; // Output: Honda
?>
3.
Inheritance
A class can inherit
properties and methods from another class.
<?php
class
Vehicle {
public $speed = "100 km/h";
public function move() {
echo
"The vehicle is moving.";
}
}
class
Car extends Vehicle {
public function honk() {
echo "Beep! Beep!";
}
}
$myCar
= new Car();
echo
$myCar->speed; // Inherited property
$myCar->move(); // Inherited method
$myCar->honk(); //
Car's own method
?>
4.
Encapsulation (Access Modifiers)
You can control access to class
properties/methods with:
- public →
Accessible everywhere.
- protected
→ Accessible only in the class and subclasses.
- private → Accessible
only inside the class.
<?php
class
BankAccount {
private $balance = 0;
public function deposit($amount) {
$this->balance += $amount;
}
public function getBalance() {
return $this->balance;
}
}
$account
= new BankAccount();
$account->deposit(500);
echo
$account->getBalance(); // Output: 500
?>
5.
Polymorphism
Different classes can have methods
with the same name, but different behavior.
<?php
class
Animal {
public function sound() {
echo "Some sound";
}
}
class
Dog extends Animal {
public function sound() {
echo "Bark!";
}
}
class
Cat extends Animal {
public function sound() {
echo "Meow!";
}
}
$dog
= new Dog();
$cat
= new Cat();
$dog->sound();
// Output: Bark!
$cat->sound();
// Output: Meow!
?>
6.
Interfaces & Abstract Classes
- Abstract classes
provide a base but cannot be instantiated directly.
- Interfaces
define methods that must be implemented in classes.
<?php
//
Interface
interface
PaymentGateway {
public function pay($amount);
}
//
Class implementing interface
class
PayPal implements PaymentGateway {
public function pay($amount) {
echo "Paid $amount using
PayPal.";
}
}
$payment
= new PayPal();
$payment->pay(100);
?>
🎯 Benefits of OOP in PHP
- Code is reusable (inheritance, interfaces).
- Projects are easier to maintain.
- Encapsulation
secures sensitive data.
- Makes applications more scalable and organized.