Welcome to Series 2 of our PHP journey! In Series 1, you built a strong foundation covering variables, functions, arrays, forms, and sessions. Now it’s time to level up with Object-Oriented Programming, starting with the most fundamental concept: PHP Classes.

This is Tutorial 1 of 10 in our PHP Advanced series. In this tutorial, you will learn what PHP Classes are, how to define them, and how to create real objects from them.

What are PHP Classes?

PHP Classes act as blueprints for creating objects. A class defines what properties (data) and methods (behavior) an object will have, but the class itself is not the object — it’s simply the template. Think of a class like a cookie cutter, and the objects it creates are the actual cookies.

Object-oriented programming organizes code around these class-based blueprints instead of scattering variables and functions independently, which becomes especially valuable as your projects grow larger and more complex.

Defining a Class

You create PHP Classes using the class keyword, followed by a name you choose, and a block of code wrapped in curly braces.

<?php
class Student {
    public $name;
    public $age;

    public function showDetails() {
        echo "Name: " . $this->name . ", Age: " . $this->age;
    }
}
?>

Inside this class, $name and $age are called properties, while showDetails() is called a method. Properties store data, and methods define actions the object can perform.

Creating an Object

A class by itself does nothing until you create an object from it. This process is called instantiation, and you use the new keyword to create an object from any of your PHP Classes.

<?php
class Student {
    public $name;
    public $age;

    public function showDetails() {
        echo "Name: " . $this->name . ", Age: " . $this->age;
    }
}

$student1 = new Student();
$student1->name = "Ahmed";
$student1->age = 22;

$student1->showDetails();
?>

Here, $student1 becomes an actual object built from the Student class. You can create as many separate objects as you need from the same PHP Classes, and each one holds its own independent data.

Understanding the $this Keyword

Inside a class, the $this keyword refers to the current object itself. It allows methods to access the properties belonging to that specific object, rather than some fixed, hardcoded value.

<?php
class Student {
    public $name;

    public function greet() {
        echo "Hello, my name is " . $this->name;
    }
}

$student1 = new Student();
$student1->name = "Sara";
$student1->greet();
?>

Without $this, the greet() method would have no way of knowing which object’s name property to reference, since PHP Classes can produce many different objects at once.

Creating Multiple Objects

One of the biggest advantages of PHP Classes is reusability. You can create multiple independent objects from the same class, and changing one object’s properties never affects another.

<?php
class Student {
    public $name;

    public function greet() {
        echo "Hello, my name is " . $this->name . "<br>";
    }
}

$student1 = new Student();
$student1->name = "Ahmed";

$student2 = new Student();
$student2->name = "Sara";

$student1->greet();
$student2->greet();
?>

Even though both objects come from the same PHP Classes blueprint, $student1 and $student2 hold completely separate name values, demonstrating how each object maintains its own state.

Methods with Parameters

Just like regular functions, methods inside PHP Classes can accept parameters, allowing them to behave dynamically based on the values passed in.

<?php
class Calculator {
    public function add($a, $b) {
        return $a + $b;
    }
}

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

echo $result;
?>

Why Use Classes and Objects?

PHP Classes help you model real-world entities — like a student, a product, or a car — in a structured, organized way. Instead of managing separate loose variables for every student’s name and age, a single class groups related data and behavior together, making your code far easier to maintain and scale. For complete technical documentation, refer to the official PHP classes and objects manual.

What’s Next?

You now understand how PHP Classes work — defining properties and methods, creating objects, and using the $this keyword. In the next tutorial, we will explore constructors, inheritance, and access modifiers, which add even more power and structure to your classes.

Explore all lessons in our PHP category, or revisit the fundamentals in our PHP Basics for Beginners series.

Practice Exercise

Complete the following tasks to reinforce what you learned in this tutorial:

  • Create a PHP file named classes-practice.php
  • Define a class called Book with properties for title and author
  • Add a method called showInfo() that echoes both properties using $this
  • Create two separate Book objects with different titles and authors
  • Call showInfo() on both objects and confirm each displays its own unique data
  • Add a method called Car class (separate from Book) with a property for brand and a method that returns a formatted string using return instead of echo
  • Create one Car object, call its method, and print the returned value

Bonus Challenge: Create a class called Rectangle with properties for width and height, plus a method called calculateArea() that returns the area. Create two Rectangle objects with different dimensions and echo both calculated areas.

About Author

Arshad Sultan

Arshad Sultan is the Founder of Tutorials Ocean and a Technical Architect & Full-Stack Developer with 15+ years of experience in PHP, Laravel, CRM development, API integrations, and AI-powered automation. He also runs Rapid Code, a software and AI automation agency, and has trained hundreds of students across Karachi's IT institutes since 2010.

Leave a Reply

Your email address will not be published. Required fields are marked *