Learning Objectives
After completing this tutorial, you will be able to:
• Understand the concepts of Object-Oriented Programming (OOP).
• Create classes and objects in PHP.
• Define properties and methods.
• Use constructors for object initialization.
• Understand access modifiers.
• Implement inheritance in PHP.
• Use the parent keyword to reuse code.
• Build simple object-oriented PHP applications.
• Use AI tools to understand and improve OOP code.
Introduction
As software applications grow larger, procedural programming becomes difficult to manage. Modern PHP applications, including those developed with Laravel, Symfony, and other frameworks, are built using Object-Oriented Programming (OOP). Object-Oriented Programming organizes code into reusable components called objects. Each object represents a real-world entity, such as a student, employee, product, or customer. These objects contain both data and behaviors. For example, a Student object may have properties such as name, ID, and department, while its methods can display student information or calculate GPA. Learning OOP is an important step toward becoming a professional PHP developer because almost every modern PHP framework is based on object-oriented concepts.
Prerequisites
Before starting this tutorial, you should understand:
• Variables
• Functions
• Arrays
• Conditional statements
• Loops
What is Object-Oriented Programming?
Object-Oriented Programming is a programming paradigm that organizes code into objects.
An object combines:
• Data
• Functions that operate on the data
Advantages of OOP include:
• Code reusability
• Better organization
• Easier maintenance
• Improved scalability
• Faster application development
What is a Class?
A class is a blueprint or template used to create objects.
For example, the blueprint of a house is not the house itself. It defines how houses should be built.
Similarly, a Student class defines the structure of every student object.
Syntax
class Student
{
}
What is an Object?
An object is an instance of a class.
<?php
class Student
{
}
$student1 = new Student();
?>
The new keyword creates an object.
Properties
Properties store data inside a class.
<?php
class Student
{
public $name;
public $department;
}
?>
Methods
Methods are functions defined inside a class.
<?php
class Student
{
public function welcome()
{
echo "Welcome to PHP OOP";
}
}
$student = new Student();
$student->welcome();
?>
Output:

Creating Objects
<?php
class Student
{
public $name;
public $department;
}
$student = new Student();
$student->name = "Meher";
$student->department = "CSE";
echo $student->name;
?>
Output:

The $this Keyword
The $this keyword refers to the current object.
<?php
class Student
{
public $name;
public function setName($studentName)
{
$this->name = $studentName;
}
public function display()
{
echo $this->name;
}
}
$student = new Student();
$student->setName("Rahim");
$student->display();
?>
Output:

Constructors
A constructor is a special method that automatically executes when an object is created.
The constructor method is named __construct().
<?php
class Student
{
public $name;
public function __construct($studentName)
{
$this->name = $studentName;
}
public function display()
{
echo $this->name;
}
}
$student = new Student("Nadia");
$student->display();
?>
Output:

Access Modifiers
Access modifiers determine where properties and methods can be accessed.
PHP provides three access modifiers.
public
Accessible from anywhere.
protected
Accessible inside the class and child classes.
private
Accessible only inside the same class.
<?php
class Employee
{
public $name = "Rahim";
protected $salary = 50000;
private $password = "abc123";
}
$emp = new Employee();
echo $emp->name();
echo $emp->salary();
echo $emp->password();
?>
Output:

Inheritance
Inheritance allows one class to inherit the properties and methods of another class.
The existing class is called the parent class.
The new class is called the child class.
Syntax
class ChildClass extends ParentClass
{
}
Example of Inheritance
<?php
class Person
{
public $name;
public function setName($name)
{
$this->name = $name;
}
}
class Student extends Person
{
public function display()
{
echo $this->name;
}
}
$student = new Student();
$student->setName("Karim");
$student->display();
?>
Output:

Using the parent Keyword
The parent keyword calls methods from the parent class.
<?php
class Animal
{
public function message()
{
echo "This is the parent class.<br>";
}
}
class Dog extends Animal
{
public function message()
{
parent::message();
echo "This is the child class.";
}
}
$dog = new Dog();
$dog->message();
?>
Output:

Method Overriding
A child class can redefine a method inherited from its parent class.
<?php
class Vehicle
{
public function start()
{
echo "Vehicle Started";
}
}
class Car extends Vehicle
{
public function start()
{
echo "Car Started";
}
}
$car = new Car();
$car->start();
?>
Output:

Example 1:
Student Information System
<?php
class Student
{
public $name;
public $id;
public function __construct($name,$id)
{
$this->name = $name;
$this->id = $id;
}
public function display()
{
echo "Name: ".$this->name."<br>";
echo "ID: ".$this->id;
}
}
$student = new Student("Ayesha","2024001");
$student->display();
?>
Output:

Example 2:
Rectangle Area Calculator
<?php
class Rectangle
{
public $length;
public $width;
public function __construct($l,$w)
{
$this->length = $l;
$this->width = $w;
}
public function area()
{
return $this->length * $this->width;
}
}
$rectangle = new Rectangle(20,10);
echo $rectangle->area();
?>
Output:

Example 3:
Employee and Manager
<?php
class Employee
{
public function work()
{
echo "Employee is working.";
}
}
class Manager extends Employee
{
public function manage()
{
echo "Manager supervises employees.";
}
}
$manager = new Manager();
$manager->work();
echo "<br>";
$manager->manage();
?>
Output:

Example 4:
Bank Account
<?php
class BankAccount
{
public $balance;
public function __construct($amount)
{
$this->balance = $amount;
}
public function deposit($amount)
{
$this->balance += $amount;
}
public function showBalance()
{
echo $this->balance;
}
}
$account = new BankAccount(5000);
$account->deposit(1000);
$account->showBalance();
?>
Output:

Example 5:
Inheritance in a University System
<?php
class Person
{
public $name;
public function __construct($name)
{
$this->name = $name;
}
}
class Teacher extends Person
{
public function display()
{
echo "Teacher: ".$this->name;
}
}
$teacher = new Teacher("Dr. Hasan");
$teacher->display();
?>
Output:

Lab Exercises
- Create a Student class that stores a student’s name, ID, department, and CGPA.
- Create a Circle class with methods to calculate area and circumference.
- Develop an Employee class with salary calculation methods.
- Create a Vehicle class and inherit Car and Motorcycle classes from it.
- Build a Product class with constructors and methods to calculate discounts.
- Develop a Library Management System using Book and Library classes.
- Create a University Management System using Person, Student, and Teacher classes.
- Build a simple Banking System with Account, SavingsAccount, and CurrentAccount classes using inheritance.
Summary
In this tutorial, you learned the fundamental concepts of Object-Oriented Programming in PHP. You created classes and objects, defined properties and methods, initialized objects using constructors, controlled access with access modifiers, and reused code through inheritance. You also explored method overriding and the parent keyword. These concepts provide the foundation for learning modern PHP frameworks such as Laravel, where object-oriented programming is used extensively to build scalable and maintainable web applications.