Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials PHP Classes & Objects
PHP Intermediate FREE

Classes & Objects

Lesson 1 of 17 Intermediate Interactive

Object-Oriented Programming (OOP) organizes code into reusable objects. PHP has full OOP support, making it ideal for building large applications.

Core OOP Concepts

  • Class — A blueprint for creating objects
  • Object — An instance of a class
  • Property — A variable inside a class (stores data)
  • Method — A function inside a class (performs actions)
  • $this — Refers to the current object instance

Visibility Modifiers

  • public — Accessible from anywhere
  • protected — Accessible within the class and its children
  • private — Accessible only within the class itself

Tip: Use private properties with getter/setter methods to control access (encapsulation).

Syntax

PHP
<?php
class Student {
    // Properties

    public string $name;
    private int $score;
    protected string $course;

    // Constructor

    public function __construct(string $name, string $course, int $score) {
        $this->name = $name;
        $this->course = $course;
        $this->score = $score;
    }

    // Method

    public function getInfo(): string {
        return "$this->name studies $this->course";
    }

    // Getter

    public function getScore(): int {
        return $this->score;
    }
}

// Create object

$student = new Student("Sukhwinder", "PHP", 95);
echo $student->getInfo();
echo $student->getScore();
?>
PHP Classes and Objects
PHP
<?php
class Car {
    private string $brand;
    private string $color;

    public function __construct(string $brand, string $color) {
        $this->brand = $brand;
        $this->color = $color;
    }

    public function describe(): string {
        return "A " . $this->color . " " . $this->brand;
    }
}

$car = new Car("Toyota", "red");
echo $car->describe() . "
";

// Visibility: private & public
class BankAccount {
    private float $balance = 0;

    public function deposit(float $amount): void {
        $this->balance += $amount;
    }

    public function balance(): float {
        return $this->balance;
    }
}

$acc = new BankAccount();
$acc->deposit(100);
$acc->deposit(50);
echo "Balance: $" . $acc->balance() . "
";
?>

Practice

1
Exercise

Create a Person class with name and age properties, a constructor, and a method that returns a greeting.

Answer
<?php
class Person {
    public string $name;
    public int $age;

    public function __construct(string $name, int $age) {
        $this->name = $name;
        $this->age = $age;
    }

    public function greet(): string {
        return "Hi, I'm " . $this->name . " and I'm " . $this->age . " years old.";
    }
}

$p = new Person("Sukhwinder", 25);
echo $p->greet();
?>
2
Exercise

Create a Calculator class with add(), subtract(), multiply(), and divide() methods.

Answer
<?php
class Calculator {
    public function add(float $a, float $b): float { return $a + $b; }
    public function subtract(float $a, float $b): float { return $a - $b; }
    public function multiply(float $a, float $b): float { return $a * $b; }
    public function divide(float $a, float $b): float { return $a / $b; }
}

$calc = new Calculator();
echo $calc->add(10, 5);
?>

Quick Quiz

1

What does $this refer to in a PHP class?

$this refers to the current instance of the object inside a class method.

2

Which visibility modifier restricts access to the class itself only?

private restricts access to within the declaring class only.

3

What method is automatically called when creating a new object?

__construct() is the constructor method called when a new object is instantiated.

Interview Questions

Encapsulation means hiding internal details and exposing only what is necessary through public methods. It protects data from unintended modification, makes code easier to maintain, and reduces coupling between components. In PHP, this is achieved using private/protected properties with getter/setter methods.

Use private when only the class itself needs access to the property — this gives maximum encapsulation. Use protected when child classes need to extend or modify the behavior — this allows inheritance while still preventing external access. The choice depends on whether subclasses need direct access to the data.