Object-Oriented
Programming (OOP) in PHP
— Study Notes
These notes provide a comprehensive, beginner-friendly yet professional explanation of Object-Oriented
Programming (OOP) in PHP.
Each topic includes concepts, analogies, PHP code examples, real-world applications, and interview relevance.
1. Introduction to OOP
Concept
Object-Oriented Programming (OOP) is a way of writing code by organizing data and behavior into reusable units called
objects. Instead of just functions and variables, we think in terms of real-world entities.
Analogy
Think of OOP as blueprints and objects. A blueprint of a car (class) can be used to make many actual cars (objects).
PHP Example
<?php
// Defining a class (blueprint)
class Car {
public $brand;
public function drive() {
echo "The car is driving.";
}
}
// Creating an object (instance)
$myCar = new Car();
$myCar->brand = "Toyota";
$myCar->drive();
Output: The car is driving.
Real-World Use Case
Used in building web applications like CMS (WordPress), where posts, users, and comments are all represented as
objects.
Interview Angle
OOP is the foundation of modern PHP frameworks (Laravel, Symfony). Interviewers expect you to know how OOP
makes code reusable and maintainable.
2. Classes and Objects
Concept
Class: Blueprint or template.
Object: Actual instance created from a class.
Analogy
A cookie cutter (class) can make many cookies (objects).
PHP Example
<?php
class Dog {
public $name;
public function bark() {
echo $this->name . " says Woof!";
}
}
$dog1 = new Dog();
$dog1->name = "Buddy";
$dog1->bark(); // Output: Buddy says Woof!
Real-World Use Case
A User class can create multiple User objects for each registered person.
Interview Angle
Shows you understand the core building blocks of OOP.
3. Properties and Methods
Concept
Properties = variables inside a class.
Methods = functions inside a class.
Analogy
A mobile phone (object) has features (properties) like brand & model and functions (methods) like call() and
message().
PHP Example
<?php
class Phone {
public $brand;
public $model;
public function call($number) {
echo "Calling $number from $this->brand $this->model.";
}
}
$phone = new Phone();
$phone->brand = "Apple";
$phone->model = "iPhone 14";
$phone->call("123456789");
Real-World Use Case
In an e-commerce site, a Product class may have properties (price, name) and methods (applyDiscount()).
Interview Angle
Tests understanding of how objects store data and behavior together.
4. Constructors and Destructors
Concept
Constructor: Special method called when an object is created.
Destructor: Special method called when an object is destroyed.
Analogy
Constructor = welcome message when entering a hotel.
Destructor = checkout process when leaving.
PHP Example
<?php
class Database {
public function __construct() {
echo "Connecting to database...";
}
public function __destruct() {
echo "Closing database connection.";
}
}
$db = new Database();
Real-World Use Case
Used for initializing database connections or cleaning up resources.
Interview Angle
Demonstrates memory management and initialization skills.
5. Inheritance
Concept
A class can inherit properties and methods from another class. Promotes code reuse.
Analogy
A child inherits traits from parents.
PHP Example
<?php
class Animal {
public function eat() {
echo "This animal is eating.";
}
}
class Cat extends Animal {
public function meow() {
echo "Meow!";
}
}
$cat = new Cat();
$cat->eat(); // Output: This animal is eating.
$cat->meow(); // Output: Meow!
Real-World Use Case
AdminUser inherits from User but has extra permissions.
Interview Angle
Interviewers test if you understand code reuse and hierarchy design.
6. Access Modifiers (public, private,
protected)
Concept
public: Accessible everywhere.
private: Accessible only within the class.
protected: Accessible in class + subclasses.
Analogy
Think of house doors: Public = open gate, Protected = locked room (family only), Private = personal locker.
PHP Example
<?php
class BankAccount {
public $owner;
private $balance = 0;
public function deposit($amount) {
$this->balance += $amount;
}
public function getBalance() {
return $this->balance;
}
}
$account = new BankAccount();
$account->owner = "John";
$account->deposit(500);
echo $account->getBalance(); // Output: 500
Real-World Use Case
Used to protect sensitive data like passwords and balances.
Interview Angle
Tests knowledge of encapsulation and security practices.
7. Static Methods and Properties
Concept
Static methods/properties belong to the class itself, not an object.
Analogy
A utility box in a workshop — available to everyone without creating a worker.
PHP Example
<?php
class MathHelper {
public static $pi = 3.14;
public static function square($n) {
return $n * $n;
}
}
echo MathHelper::$pi; // Output: 3.14
echo MathHelper::square(4); // Output: 16
Real-World Use Case
Math libraries, logging systems, utility functions.
Interview Angle
Shows you understand when to use class-level utilities.
8. Constants
Concept
Constants are fixed values in a class (cannot be changed).
Analogy
Like a birthday date — it never changes.
PHP Example
<?php
class Config {
const DB_NAME = "my_database";
}
echo Config::DB_NAME;
Real-World Use Case
Database credentials, configuration keys.
Interview Angle
Tests if you know immutable values in classes.
9. Abstract Classes and Methods
Concept
Abstract classes cannot be instantiated.
They provide a base structure for other classes.
Abstract methods must be implemented in child classes.
Analogy
Think of government laws — they define rules but local authorities (child classes) must implement them.
PHP Example
<?php
abstract class Shape {
abstract public function area();
}
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();
Real-World Use Case
Defining a PaymentGateway abstract class with concrete implementations like PayPal and Stripe.
Interview Angle
Tests design abstraction skills.
... (Remaining sections would follow in same format: Interfaces, Traits, Method Overriding, Object Cloning, Type Hinting,
Late Static Binding, Final, Polymorphism, Encapsulation, Namespaces, Exception Handling, Autoloading, Design
Patterns, SOLID) ...