As web applications grow from simple scripts into enterprise software platforms, procedural code quickly becomes unmanageable. Object-Oriented Programming (OOP) solves this by bundling data (properties) and functions (methods) into cohesive, self-contained units called Objects. All modern PHP frameworks—including Laravel, Symfony, and WordPress—are architected entirely on OOP principles.
Figure 7: The Class Blueprint defines properties and methods, while multiple unique Objects exist in memory.
A Class is declared using the class keyword. Variables declared inside a class are called Properties, and functions declared inside a class are called Methods:
<?php
class Student {
// Properties (Attributes)
public string $name;
public string $rollNumber;
public float $marks;
// Method (Behavior)
public function displayDetails(): void {
// $this refers to the current calling object instance
echo "Student: " . $this->name . " (Roll: " . $this->rollNumber . ") - Marks: " . $this->marks . "%<br>";
}
}
// Creating (instantiating) object instances using 'new'
$student1 = new Student();
$student1->name = "Vikram Aditya";
$student1->rollNumber = "BCA-101";
$student1->marks = 94.5;
$student1->displayDetails();
$student2 = new Student();
$student2->name = "Anjali Rao";
$student2->rollNumber = "BCA-102";
$student2->marks = 91.0;
$student2->displayDetails();
?>
__construct) and Destructors (__destruct)Manually setting properties one-by-one is tedious. A Constructor is a special magic method that PHP executes automatically the moment an object is instantiated:
<?php
class Course {
public string $title;
public string $instructor;
public int $durationWeeks;
// Constructor executed automatically on 'new Course(...)'
public function __construct(string $title, string $instructor, int $durationWeeks = 12) {
$this->title = $title;
$this->instructor = $instructor;
$this->durationWeeks = $durationWeeks;
echo "Course '{$this->title}' successfully initialized.<br>";
}
// Destructor executed when object is destroyed or script ends
public function __destruct() {
echo "Closing course session for: '{$this->title}'<br>";
}
}
// Instantiate with arguments passed directly to the constructor
$phpCourse = new Course("PHP 8 Master Class", "Veewom Faculty", 16);
?>
In PHP 8, you can combine property declaration, parameter definitions, and assignment into a single line, eliminating dozens of lines of repetitive boilerplate code:
<?php
// OLD verbose PHP 7 way required declaring properties, parameters, and $this assignments
// MODERN PHP 8 Way: Constructor Property Promotion
class UserAccount {
public function __construct(
public string $username,
public string $email,
public string $role = "Student"
) {} // Empty body! PHP automatically creates and assigns the properties!
}
$user = new UserAccount("rohan_m", "rohan@veewom.com");
echo "User created: " . $user->username;
?>
High-Frequency Exam Question:
Q: What is the purpose of the instanceof operator in PHP?
A: The instanceof operator checks whether a specific object is an instance of a given class or implements a specific interface:
if ($student1 instanceof Student) { /* valid object */ }.
Exercise 35.1: Create a BankAccount class with properties for account number and balance, and methods deposit(float $amount) and withdraw(float $amount).
Exercise 35.2: Implement the class using PHP 8's Constructor Property Promotion feature.