Classes & Objects in PHP

Introduction

In Object-Oriented Programming (OOP), classes and objects are the foundation of everything. Understanding these two concepts clearly will make the rest of OOP much easier to learn.

In simple terms, a class is a blueprint, and an object is a real instance created from that blueprint. In this tutorial, you will learn what classes and objects are in PHP, how to create them, and how they work together.

What is a Class in PHP?

A class is a blueprint or template used to create objects.
It defines properties (variables) and methods (functions) that an object will have.

Think of a class as a design, not a real thing. It does not represent a real thing by itself — it only defines what the object will look like and what it can do.

Example:
A class called User defines:

  • name
  • email
  • login behavior

What is an Object in PHP?

An object is a real instance of a class.

If a class is a blueprint of a house,
then an object is the actual house built from that blueprint.

You can create multiple objects from the same class.

Creating a Class in PHP

Here is a simple example of a class:

<?php
class User {
    public $name;
    public $email;

    public function sayHello() {
        echo "Hello, welcome!";
    }
}
?>

Explanation:

  • class User → defines a class
  • $name, $email → properties
  • sayHello() → method

Creating an Object from a Class

To use a class, we create an object using the new keyword.

<?php
$user1 = new User();
?>

Now $user1 is an object of the User class.

Accessing Properties and Methods

You can access properties and methods using -> operator.

<?php
$user1 = new User();
$user1->name = "Divyesh";
$user1->email = "div@example.com";

echo $user1->name;
$user1->sayHello();
?>

The $this Keyword

$this refers to the current object inside a class.

It is used to access:

  • properties
  • methods

Example:

<?php
class User {
    public $name;

    public function setName($username) {
        $this->name = $username;
    }

    public function getName() {
        return $this->name;
    }
}
?>

Using $this with Object

<?php
$user = new User();
$user->setName("Divyesh");

echo $user->getName();
?>

Multiple Objects from One Class

<?php
$user1 = new User();
$user2 = new User();

$user1->setName("Amit");
$user2->setName("Rahul");

echo $user1->getName();
echo $user2->getName();
?>

Each object has its own data, even though the class is the same.

Real-World Analogy

Let’s understand this with a simple example:

  • Class → Car blueprint
  • Object → Actual car
  • Properties → Color, speed
  • Methods → Start(), Stop()

You can manufacture many cars from the same blueprint. Each car has its own color and speed but follows the same design.

Similarly, a class defines structure, and objects represent real entities based on that structure.

Why Classes & Objects are Important?

They help you:

  • Organize code properly
  • Avoid repetition
  • Represent real-world entities
  • Build scalable applications
  • Reduce repetition
  • Write reusable logic

Almost all modern PHP applications use classes and objects.

Common Mistakes Beginners Make

  • Forgetting to use the new keyword
  • Using . instead of -> for accessing properties
  • Forgetting to declare properties before using them
  • Confusing class name with object variable

Practice small examples to avoid these mistakes.

Practical Task

Create a class named Car with properties like brand and color.
Add a method called start() that prints a message.
Create two objects from this class and assign different values to each object.

Observe how both objects use the same class but store different data.

Summary

  • A class is a blueprint or template
  • An object is an instance of a class
  • Properties store data
  • Methods define behavior
  • Multiple objects can be created from a single class

Understanding classes and objects is essential before learning constructors, inheritance, and other advanced OOP concepts.

In the next tutorial, we’ll learn about Constructors & Destructors in PHP.

Related Tutorials

  • Introduction to Object-Oriented Programming (OOP) in PHP

    Introduction As PHP projects grow, managing code using simple procedural programming becomes difficult. Files become longer, functions are scattered, and maintaining the project becomes time-consuming. This is where Object-Oriented Programming (OOP) becomes useful. OOP helps you organize your PHP code in a structured way by grouping related data and functionality together. Instead of writing everything…

  • Constructors & Destructors in PHP

    Introduction In Object-Oriented Programming, constructors and destructors are special methods that automatically run at specific moments in an object’s lifecycle. A constructor runs when an object is created.A destructor runs when an object is destroyed. These methods help initialize and clean up resources in a PHP application. Understanding them is essential for writing structured and…

  • Access Modifiers in PHP (public, private, protected)

    Introduction In Object-Oriented Programming, access modifiers control the visibility of properties and methods inside a class. They determine: In PHP, there are three main access modifiers: Understanding access modifiers is important because they help protect data and improve code security and structure. Why Are Access Modifiers Important? Without access control, anyone could change the internal…

  • Inheritance in PHP

    Introduction Inheritance is one of the most important concepts in Object-Oriented Programming (OOP). It allows one class to inherit the properties and methods of another class. In simple words, inheritance helps you reuse existing code instead of rewriting it. This makes your code cleaner, more structured, and easier to maintain. In this tutorial, you will…

  • Encapsulation in PHP

    Introduction Encapsulation is one of the core principles of Object-Oriented Programming (OOP). It refers to the concept of restricting direct access to certain parts of an object and allowing controlled access through methods. In simple words, encapsulation means data hiding. Instead of allowing users to directly access and modify object properties, we protect them using…

  • Polymorphism in PHP

    Introduction Polymorphism is one of the four core principles of Object-Oriented Programming (OOP): The word Polymorphism means “many forms.” In PHP, polymorphism allows different classes to use the same method name but perform different actions. This makes your code flexible, scalable, and easier to maintain. 📌 What is Polymorphism? Polymorphism allows objects of different classes…

Leave a Reply

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