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
- 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→ propertiessayHello()→ 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
newkeyword - 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.
