Object-Oriented Programming in PHP
Object-Oriented Programming in PHP
Object-oriented programming (OOP) is a way of organising code around "objects" that bundle related data and behaviour together. In PHP you define a class as a blueprint, then create objects from it. OOP keeps larger applications tidy, reusable, and easier to maintain.
Classes and objects
A class describes what an object has (properties) and what it can do (methods). An object is a specific instance built from that class.
<?php
// Define a class (the blueprint).
class Student {
// Properties: data the object holds.
public string $name;
public int $age;
// Method: behaviour the object can perform.
public function introduce(): string {
return "Hi, I am {$this->name}, age {$this->age}.";
}
}
// Create an object (instance) from the class.
$s = new Student();
$s->name = "Nikhil";
$s->age = 19;
echo $s->introduce(); // Hi, I am Nikhil, age 19.
?>
$this refers to the current object inside a method, and the -> operator accesses properties and methods.
Constructors
A constructor is a special method named __construct() that runs automatically when an object is created. It is the natural place to set up initial values.
<?php
class Course {
public string $title;
// Runs when "new Course(...)" is called.
public function __construct(string $title) {
$this->title = $title;
}
}
$c = new Course("PHP & MySQL");
echo $c->title; // PHP & MySQL
?>
Constructor property promotion (PHP 8)
PHP 8 lets you declare and assign properties directly in the constructor, cutting boilerplate.
<?php
class Course {
// Declaring and assigning in one step.
public function __construct(
public string $title,
public int $durationWeeks = 8
) {}
}
$c = new Course("Web Development");
echo "{$c->title} runs for {$c->durationWeeks} weeks.";
?>
Visibility: public, private, protected
Visibility controls who can access a property or method.
public— accessible from anywhere.private— accessible only inside the same class.protected— accessible inside the class and its child classes.
<?php
class Account {
private float $balance = 0;
public function deposit(float $amount): void {
$this->balance += $amount; // allowed: inside the class
}
public function getBalance(): float {
return $this->balance;
}
}
$a = new Account();
$a->deposit(500);
echo $a->getBalance(); // 500
// echo $a->balance; // Error: balance is private
?>
Keeping data private and exposing it through methods is called encapsulation, a core OOP idea.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesInheritance
A class can extend another to reuse its features and add new ones.
<?php
class Person {
public function __construct(public string $name) {}
public function greet(): string {
return "Hello, I am {$this->name}.";
}
}
// Teacher inherits everything from Person.
class Teacher extends Person {
public function teach(): string {
return "{$this->name} is teaching PHP.";
}
}
$t = new Teacher("Atul");
echo $t->greet(); // inherited method
echo $t->teach(); // new method
?>
Static members and constants
Static properties and methods belong to the class itself, not to an object.
<?php
class Counter {
public static int $count = 0;
public static function increment(): void {
self::$count++;
}
}
Counter::increment();
Counter::increment();
echo Counter::$count; // 2
?>
Common mistakes
- Forgetting
$this->when accessing a property inside a method. - Confusing the class (blueprint) with the object (instance). You define one class but can create many objects.
- Making everything
public. Preferprivatefor internal data and expose it through methods. - Mixing up
->(objects) and::(static/class-level). Use::for static members and constants.
FAQ
What is the difference between a class and an object? A class is the blueprint; an object is a concrete thing built from that blueprint. One class can produce many objects.
Why use private properties? Encapsulation protects data from accidental changes and lets you control how values are read or updated through methods.
Do I need OOP for small scripts? Not always. Small scripts can be procedural. OOP shines as projects grow and you want reusable, organised code.
Keep learning
Back to the PHP & MySQL hub, revisit Strings in PHP, or continue to Handling Forms in PHP.
Build object-oriented projects with mentor reviews. Join the waitlist for the PHP course at Infoplanet in Jalgaon.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesFounder, Infoplanet
Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.
Related guides
Arrays in PHP: Indexed, Associative & Multidimensional
Understand PHP arrays - indexed, associative, and multidimensional - and the most useful array functions, with examples for adding, reading, and looping over data.
Control Flow in PHP: if, switch & Loops
Make PHP code decide and repeat with control flow: if/else, switch, the match expression, and the for, while, and foreach loops, with clear examples.
Handling Forms in PHP (GET & POST)
Handle HTML forms in PHP: the difference between GET and POST, reading input from the superglobals, validating data, and escaping output to prevent XSS.
