New! Explore our Programming Academy and AI Tutor - learn to code from scratch, free to start. Explore Now
Programming› PHP› Classes, Objects and Methods

Classes, Objects and Methods

💻 PHP

Group related data and behaviour into classes, and create reusable objects.

Lesson 6 of 9 Tutorial
0 of 9 completed

A class is a blueprint. An object is one thing built from that blueprint. Classes keep related data and the functions that work on it in one place.

By the end of this lesson you can:
  • Define a class with properties and methods.
  • Create objects with new.
  • Use constructors and visibility.

Your First Class

A class holds properties (data) and methods (functions). Use $this inside a method to reach the current object.

<?php

class Student
{
    public string $name;
    public int $score = 0;

    public function addScore(int $points): void
    {
        $this->score += $points;
    }

    public function grade(): string
    {
        return match (true) {
            $this->score >= 70 => 'A',
            $this->score >= 60 => 'B',
            $this->score >= 50 => 'C',
            default => 'F',
        };
    }
}

$student = new Student();
$student->name = 'Amina';
$student->addScore(72);

echo $student->name . ' scored ' . $student->grade();

Constructors

A constructor runs when the object is created. It is the right place to require the values an object needs to be valid.

<?php

class Student
{
    public function __construct(
        private string $name,
        private int $score = 0,
    ) {
    }

    public function name(): string
    {
        return $this->name;
    }
}

$student = new Student('Bola', 65);
echo $student->name();

Visibility

Use public for things outside code may use, private for internal details, and protected for subclasses. Prefer private properties with public methods so the object controls its own state.

Tip Keep classes small and focused. A class called Student should not also send emails, write files and render HTML.

Practice

  1. Create a BankAccount class with deposit and withdraw methods.
  2. Stop a withdrawal that is larger than the balance.
  3. Add a constructor that requires the account holder name.

Hands-on exercise

Create a class named Product.
1. It takes a name and a price in the constructor.
2. It has a method priceWithVat() that returns price × 1.075, rounded to two decimals.
3. Create an object and print the result.

Recap

Classes group data and behaviour. Constructors set up valid objects, and visibility protects internal state.

Example

<?php

class Student
{
    public string $name;
    public int $score = 0;

    public function addScore(int $points): void
    {
        $this->score += $points;
    }

    public function grade(): string
    {
        return match (true) {
            $this->score >= 70 => 'A',
            $this->score >= 60 => 'B',
            $this->score >= 50 => 'C',
            default => 'F',
        };
    }
}

$student = new Student();
$student->name = 'Amina';
$student->addScore(72);

echo $student->name . ' scored ' . $student->grade();

Exercises

Exercise 1. A product class Medium
Create a class named Product.
1. It takes a name and a price in the constructor.
2. It has a method priceWithVat() that returns price × 1.075, rounded to two decimals.
3. Create an object and print the result.
PHP exercise-1.php
Expected Output
2687.5

Lesson Nav

Lesson sections