Introduction to Object-Oriented PHP (OOP)
What is Object-Oriented Programming in PHP?
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of “objects”, which bundle data (properties) and functions (methods) into single reusable structures.
Instead of writing procedural code in a top-to-bottom sequence, OOP lets you model real-world entities and reuse code more efficiently.
Benefits of OOP:
- Better code organization and structure
- Encourages modularity and reusability
- Easier to maintain and scale large applications
- Common in PHP frameworks like Laravel, Symfony, and CodeIgniter
Basic Structure of a PHP Class
Defining a Class:
class Car {
public $brand = "Toyota";
public function honk() {
return "Beep!";
}
}
Creating an Object:
$myCar = new Car();
echo $myCar->brand; // Output: Toyota
echo $myCar->honk(); // Output: Beep!
A class is like a blueprint, and an object is a real instance created from it.
Properties and Methods
- Properties are variables inside a class
- Methods are functions defined inside a class
Example:
class Person {
public $name;
public function sayHello() {
return "Hello, my name is " . $this->name;
}
}
$user = new Person();
$user->name = "Emma";
echo $user->sayHello(); // Output: Hello, my name is Emma
$this
refers to the current object inside the class.
Constructors and Destructors
Constructor:
Runs automatically when an object is created. Used to initialize values.
class User {
public $name;
public function __construct($name) {
$this->name = $name;
}
}
$user1 = new User("Jay");
echo $user1->name; // Output: Jay
Destructor:
Runs when the object is destroyed (end of script or manually unset).
public function __destruct() {
echo "Object destroyed.";
}
Access Modifiers: public
, private
, protected
They control visibility of properties and methods:
Modifier | Accessible from |
---|---|
public | Anywhere |
private | Inside the class only |
protected | Inside the class and subclasses |
Example:
class BankAccount {
private $balance = 1000;
public function getBalance() {
return $this->balance;
}
}
$acc = new BankAccount();
// echo $acc->balance; ❌ Error: private property
echo $acc->getBalance(); // ✅ Output: 1000
Real-World Example: Simple Calculator Class
class Calculator {
public function add($a, $b) {
return $a + $b;
}
public function subtract($a, $b) {
return $a - $b;
}
}
$calc = new Calculator();
echo $calc->add(10, 5); // Output: 15
echo $calc->subtract(10, 5); // Output: 5
Example: User Profile Object
class User {
public $name;
public $email;
public function __construct($name, $email) {
$this->name = $name;
$this->email = $email;
}
public function getProfile() {
return "Name: $this->name | Email: $this->email";
}
}
$user1 = new User("Emma", "emma@example.com");
echo $user1->getProfile();
Common Mistakes to Avoid
- Forgetting to use
$this->
inside methods - Directly accessing
private
orprotected
properties from outside the class - Not using constructors to initialize required properties
- Mixing procedural and object-oriented code without structure
Best Practices for OOP in PHP
- Use camelCase for property and method names
- Keep properties
private
orprotected
, expose via getter/setter - Use constructors to enforce necessary values
- Keep each class focused on one purpose (Single Responsibility Principle)
- Favor composition over inheritance (covered later)
Notes:
- Object-Oriented PHP helps build scalable, modular, and maintainable code
- Classes define blueprints; objects are real instances
- Use constructors to initialize objects and access modifiers to protect data
- OOP is the foundation of modern PHP frameworks and applications
Practice Tasks
Task 1: Book Class
Create a Book
class with:
- Properties:
title
,author
- Constructor to initialize both
- Method to return formatted string: “Title by Author”
Task 2: BankAccount Class
- Private property
balance
- Public method
deposit($amount)
andgetBalance()
Task 3: Auto Profile Generator
Create a Profile
class that takes name
, age
, and location
and displays:Jay is 30 years old and lives in Mumbai.
💡 Explore More PHP Learning Tools & Resources
PHP Practice Quiz
Test your PHP skills with real coding questions and scoring.
PHP Interview Questions
Prepare for interviews with common PHP questions and answers.
Educational AI Tutor
Ask PHP questions and get instant AI-powered explanations.
Question Generator
Auto-generate PHP interview questions for quick practice.