Classes & Objects
Learn how to define a PHP class, add properties and methods, and create objects from it using the new keyword.
Introduction
The previous lesson introduced OOP conceptually — what it is and why it matters. Now it is time to write real class syntax. In this lesson, you will define your first PHP class, give it properties and methods, and create actual objects from it.
By the end, you will be comfortable with the core mechanics that every other OOP topic in this course builds on: the class keyword, properties, methods, new, the -> operator, and $this.
- How to define a class using the class keyword.
- How to add properties and methods to a class.
- How to create an object from a class using new.
- How to access properties and call methods with the -> operator.
- What $this refers to inside a class.
- How to create multiple independent objects from the same class.
Defining a Class
A class is defined with the class keyword, followed by a name and a pair of curly braces containing its properties and methods. By convention, class names use PascalCase (each word capitalized, no underscores).
<?php
class Car {
// properties and methods will go here
}On its own, this class does not do much yet — it is just an empty blueprint. The next step is to give it some data (properties) and behavior (methods).
Properties and Methods
Properties are variables that belong to a class, representing the data each object will hold. Methods are functions that belong to a class, representing what an object can do.
<?php
class Car {
// Properties
public string $brand = "Unknown";
public int $speed = 0;
// Method
public function accelerate(int $amount): void {
$this->speed += $amount;
}
}Here, Car has two properties (brand and speed) and one method (accelerate). The public keyword is an access modifier, meaning this property or method can be used from outside the class; access modifiers get a full lesson of their own soon.
Creating an Object with new
A class by itself is just a blueprint — it does not do anything until you create an object (also called an "instance") from it using the new keyword.
<?php
class Car {
public string $brand = "Unknown";
public int $speed = 0;
public function accelerate(int $amount): void {
$this->speed += $amount;
}
}
$myCar = new Car();
var_dump($myCar);object(Car)#1 (2) {
["brand"]=>
string(7) "Unknown"
["speed"]=>
int(0)
}$myCar now holds a real Car object, with its own copy of the brand and speed properties, ready to be used.
Accessing Members with ->
Once you have an object, you use the arrow operator (->) to read or change its properties, and to call its methods.
<?php
class Car {
public string $brand = "Unknown";
public int $speed = 0;
public function accelerate(int $amount): void {
$this->speed += $amount;
}
}
$myCar = new Car();
$myCar->brand = "Toyota";
$myCar->accelerate(30);
echo $myCar->brand;
echo "\n";
echo $myCar->speed;Toyota
30Notice that $myCar->brand = "Toyota" set the property directly, while $myCar->accelerate(30) called a method that changed speed internally.
The -> operator accesses properties and methods on an object instance, like $myCar->brand. The :: operator (covered later) is used for static members that belong to the class itself rather than any one object.
Understanding $this
Inside a class's methods, the special variable $this refers to the current object the method is being called on. It is how a method reaches its own object's properties, or calls its own other methods.
<?php
class Car {
public string $brand = "Unknown";
public int $speed = 0;
public function accelerate(int $amount): void {
$this->speed += $amount; // $this refers to whichever Car called accelerate()
}
public function describe(): string {
return "{$this->brand} is going {$this->speed} km/h";
}
}
$myCar = new Car();
$myCar->brand = "Honda";
$myCar->accelerate(45);
echo $myCar->describe();Honda is going 45 km/hWithout $this, a method would have no way to know which object's data it should read or modify — it lets the same method code work correctly no matter which object called it.
Creating Multiple Objects
The real power of a class is that you can create as many independent objects from it as you need, each with its own separate property values.
<?php
class Car {
public string $brand = "Unknown";
public int $speed = 0;
public function accelerate(int $amount): void {
$this->speed += $amount;
}
}
$car1 = new Car();
$car1->brand = "Toyota";
$car1->accelerate(20);
$car2 = new Car();
$car2->brand = "Ford";
$car2->accelerate(60);
echo "{$car1->brand}: {$car1->speed} km/h\n";
echo "{$car2->brand}: {$car2->speed} km/h";Toyota: 20 km/h
Ford: 60 km/h$car1 and $car2 are two completely separate objects. Changing one's speed or brand has no effect on the other, even though both were created from the exact same Car class.
This is one of the most useful aspects of OOP: you write the Car class once, and every new Car() gives you a fresh, independent object built from that same design.
Common Mistakes
- Forgetting the -> operator and trying to access a property like a plain variable, e.g. $brand instead of $myCar->brand.
- Forgetting $this inside a method and trying to reference a property directly, e.g. $speed instead of $this->speed.
- Confusing the class name (the blueprint, like Car) with a variable holding an object (like $myCar).
- Assuming two objects created from the same class share the same data — each object has its own independent properties.
Best Practices
- Name classes with PascalCase (e.g. Car, UserAccount) to visually distinguish them from variables and functions.
- Give properties sensible default values where it makes sense, so a freshly created object is always in a valid state.
- Use $this consistently inside methods when referring to the current object's own properties.
- Keep a class focused on one clear concept — a Car class should describe cars, not unrelated concerns.
Frequently Asked Questions
What is the difference between a class and an object?
A class is the blueprint or definition (like Car). An object is an actual instance created from that blueprint using new Car(), with its own real property values.
Can I access a property without creating an object first?
Not with regular (non-static) properties. You must first create an object with new, then access its properties through that object using ->.
What does $this refer to exactly?
Inside a method, $this refers to the specific object that method was called on — so $this->speed always refers to that particular object's speed, not any other object's.
Can two objects from the same class have different property values?
Yes, absolutely. Each object gets its own independent copy of the class's properties, so changing one object never affects another.
Do I always need to write public in front of properties and methods?
For now, yes, to keep things simple and accessible. The Access Modifiers lesson explains when to use protected or private instead.
Key Takeaways
- A class is defined with the class keyword and acts as a blueprint for objects.
- Properties hold an object's data; methods define what an object can do.
- You create an object from a class using new ClassName().
- The -> operator accesses properties and calls methods on an object.
- $this inside a method refers to the specific object the method was called on.
- Multiple objects created from the same class are completely independent of one another.
Summary
You have now written your first real PHP classes and objects — defining properties and methods, creating instances with new, accessing members with ->, and understanding how $this ties a method back to its own object. These mechanics form the foundation for everything else in object-oriented PHP.
Next, you will learn about constructors and destructors — special methods that run automatically when an object is created or destroyed, letting you set up and clean up an object's state without calling extra methods manually.
- You can define a class with properties and methods.
- You can create objects using new.
- You can access and modify object members with ->.
- You understand what $this refers to inside a method.
- You can create multiple independent objects from one class.