MasterPHP.in
PHP Tutorial

OOP Basics in PHP


So far, you’ve written PHP code using variables, arrays, and functions — this is called procedural programming.

But as your application grows, managing data like users, products, and orders becomes messy.


The Problem (Procedural Code Gets Messy)

$user1_name = "Rohan";
$user1_age = 22;

$user2_name = "Amit";
$user2_age = 25;

function showUser1() {
    global $user1_name, $user1_age;
    echo $user1_name . " - " . $user1_age;
}

function showUser2() {
    global $user2_name, $user2_age;
    echo $user2_name . " - " . $user2_age;
}

👉 Even with just 2 users, code starts repeating and becomes hard to manage.


The Solution: OOP

Object-Oriented Programming lets you group related data and behavior together.

Instead of separate variables and functions, you create objects based on a class blueprint.


Class vs Object

  • Class → Blueprint (defines structure)
  • Object → Instance (actual data)

👉 You write a class once, but you can create many objects from it — each with its own data.

Real-Life Analogy

  • Class → Car blueprint
  • Object → BMW, Audi

👉 Just like a car blueprint defines what every car will have (brand, color), a PHP class defines what every object will contain.


Creating a Class

class Car {
    public $brand;
    public $color;
}

👉 A class contains properties (variables).

What is public?

public means the property or method can be accessed from anywhere.

👉 You’ll learn private and protected in upcoming lessons.


Creating an Object

$car1 = new Car();

👉 The new keyword creates an object from a class.


Setting and Accessing Properties

$car1->brand = "BMW";
$car1->color = "Black";

echo $car1->brand;

Output:

BMW

👉 This works, but it's not ideal.

If you forget to set a property before using it, your program may behave unexpectedly.

👉 In the next lesson, you'll learn how constructors solve this by requiring values upfront.


Methods in PHP

Methods are functions inside a class.

Example

class Car {
    public $brand;

    public function start() {
        echo "Car started";
    }
}

Calling a Method

$car1 = new Car();
$car1->start();

Output:

Car started


Understanding $this (Important)

$this refers to the current object.

Why $this is Needed

$car1 = new Car();
$car1->brand = "BMW";

$car2 = new Car();
$car2->brand = "Audi";

👉 Now PHP needs to know: which object's data should be used?

Example

class Car {
    public $brand;

    public function showBrand() {
        echo $this->brand;
    }
}

$car1 = new Car();
$car1->brand = "BMW";

$car2 = new Car();
$car2->brand = "Audi";

$car1->showBrand(); // BMW
$car2->showBrand(); // Audi

👉 $this ensures the method uses the correct object's data.


Multiple Objects (Key Concept)

One class can create multiple independent objects.

class Product {
    public $name;
    public $price;

    public function display() {
        echo $this->name . " - ₹" . $this->price;
    }
}

$product1 = new Product();
$product1->name = "Laptop";
$product1->price = 50000;

$product2 = new Product();
$product2->name = "Phone";
$product2->price = 20000;

$product1->display();
$product2->display();

Output:

Laptop - ₹50000
Phone - ₹20000

👉 Each object has its own independent data.


Common Mistakes Beginners Make

1. Forgetting $this

public function show() {
    echo $name; // Error
}

Correct:

public function show() {
    echo $this->name;
}

2. Not Using new

$car1 = Car(); // Wrong

Correct:

$car1 = new Car();

3. Wrong Property Access

echo $car1.brand; // Wrong

Correct:

echo $car1->brand;


Practice Exercise

Task 1 (Easy)

Create a class User with a property name.

Create an object and print:

Rohan

Task 2 (Medium)

Create a class User with properties name and email.

Add a method introduce() that prints:

Hi, I'm Rohan and my email is rohan@example.com

Task 3 (Hard)

Create a class BankAccount with:

  • property owner
  • property balance

Add methods:

  • deposit($amount) → increases balance
  • showBalance() → prints balance

Expected Output:

Balance: ₹5000


Summary

In this lesson, you learned:

  • classes and objects
  • properties and methods
  • the public keyword
  • how $this works
  • how one class can create multiple objects

These are the core building blocks of Object-Oriented Programming in PHP.

Share this tutorial