Introduction
In Object-Oriented Programming (OOP), classes and objects are the foundation of everything. Once you understand these two concepts clearly, learning advanced OOP topics like constructors, inheritance, and polymorphism becomes much easier.
In simple words, a class is a blueprint, and an object is a real instance created from that blueprint.
For example, if βCarβ is a class, then each individual car you create becomes an object. The class defines what the car can have, such as color, speed, and brand, while the object stores actual values.
This approach helps developers write clean, reusable, and scalable code.
What You’ll Learn
In this tutorial, you’ll learn about:
- what a class is
- what an object is
- how to create classes in PHP
- how to create objects using the new keyword
- how properties and methods work together
π Classes and objects are the first real step into professional PHP OOP development.
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.
FAQs
A class is a blueprint or template used to create objects. It defines properties (variables) and methods (functions).
An object is a real instance created from a class. Multiple objects can be created from the same class.
A class is a design or blueprint, while an object is the actual usable instance created from that blueprint.
They help organize code, reduce repetition, improve reusability, and make applications easier to maintain.
$this refers to the current object inside a class and is used to access its properties and methods.
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.
Conclusion
Classes and objects are the foundation of Object-Oriented Programming in PHP.
A class acts as a blueprint, while objects are real instances created from that blueprint. Using classes and objects helps developers write cleaner, more organized, and reusable code.
This becomes especially important when building large applications like admin panels, custom PHP projects, WordPress plugins, and frameworks like Laravel.
Once you understand classes and objects well, learning advanced OOP concepts becomes much easier.
Start with small examples, practice creating your own classes, and gradually move toward real-world project development.
π Mastering classes and objects is the first major step toward becoming a strong PHP developer.
In the next tutorial, weβll learn about Constructors & Destructors in PHP.
