Table of Contents
6. Objects — When Data Deserves a Name
Up to now, we've worked with arrays
— simple containers for values.
But sometimes the data we're working with is more than a list or a map.
It's a “thing” — something with more meaning.
When our data starts to feel like a “thing”, it deserves a name.
That's where objects come in as containers.
1. Before Any Code: What Is an Object?
Imagine a folder on our desk
containing information about a person,
a user, a client… just an example!
Inside the folder, we keep:
- a name
- an age
- a note about whether someone is active
The folder groups these pieces of information together
because they belong to the same idea: a user or a person's profile.
An object is just that —
an existing named container
for related information.
And it could be one of many similar objects.
Similar here means they were created from the same class.
2. A Class Is the Blueprint
If an object is an actual folder,
a class is the template idea used to create folders of the same kind.
We can think of the idea like:
User ├── name ├── age └── is active?
This is the shape of a, for example, User class.
A class describes the shape of future objects.
An object is an actual instance of that shape.
An instance is one actual thing built from the class
— a living copy of the idea.
3. The Simplest Class (No Behavior Yet)
Here's what that folder class representation looks like in PHP:
class User { public string $name; public int $age; }
Let's slow down and explain:
class User— we are defining a blueprint called “User”- inside it, we list the pieces of data an object of type User must have
$namecan hold values of type string$agecan hold values of type integer
This represents the simplest possible object:
a named container
for related data.
No behavior yet.
Just shape of potential objects.
4. Creating an Object From the Blueprint
Once we have a class, we can create one or more actual users
— real, individual “folders” based on the blueprint.
$user1 = new User(); $user1->name = "Tony"; $user1->age = 99;
Here's what's happening, step by step:
new User()creates a fresh object of type User and stores it in the variable $user1
Imagine a fresh “User” folder on our desk with a name $user1.$user1is the label we put on that folder
It's just the name we chose for this particular instance.$user1→namemeans “open the folder and look at the name inside”
The arrow→is PHP's way of saying:
“Go inside this object and find this item.”$user1→agemeans “open the folder and look at the age inside”
Same arrow, same idea — look inside the object.
The whole flow reads like a small story:
"Create a new User folder. Label it $user1. Open it and set the name. Open it and set the age".
This is the moment where objects stop being abstract
and start feeling like
something we can hold in our hands.
5. Adding Behavior — What the Object Can Do
So far, our object is just a folder holding information.
But objects can also do things.
They can answer questions, perform small checks, or carry simple rules that belong to the concept.
These actions are called methods.
Let's add one to our User class.
class User { public string $name; public int $age; public function isAdult(): bool { return $this->age >= 18; } }
Let's slow down and unpack this gently.
public function isAdult()
Here we define a method called isAdult — something the user can do.
In the example, the method answers a yes/no question:
“Is the user an adult?” we use a yes/no expression here (called a boolean).return
It means: “give back the result for further processing”.$this
The $this keyword is PHP's way of saying:
“the object we're inside right now”.
If we imagine the folder again, $this means “this folder”.$this→age
It means: “open this folder and look at the age inside”.>= 18It checks whether the age is at least 18.
It's a simple comparison that returns a boolean result oftrueorfalse.
Put together, the method reads like a sentence:
“Look at this user's age and return true if it's 18 or more”.
That's all a method is — a small piece of behavior that belongs to the object.
6. Why Behavior Belongs Inside the Object
If we think about it,
the rule “a user is an adult if their age is at least 18”
belongs with the user.
It's part of what the concept means.
By placing the rule inside the class:
- the idea stays in one place
- the code becomes easier to read
- we avoid repeating the same logic everywhere
- the object becomes more meaningful
Objects are not just containers
— they are small bundles of meaning .
7. When Objects Are the Right Tool
Use an object when:
- the data belongs together
- the idea feels real or concrete
- the concept might need behavior
- naming the idea makes the code clearer
- the meaning matters more than the mechanics
Objects help our code read like a story:
“Create a user.
Set their name.
Check if they are an adult”.
8. When Objects Are Too Much
Not everything needs a class.
If the data is:
- tiny
- temporary
- not meaningful
- just configuration
- not reused
…an array is often enough.
Objects are for concepts.
Arrays are for “just values”.
9. The Mental Model
Objects are named containers for meaningful data.
They can also carry small actions that belong to the concept.
Use them when:
- the idea is real
- the data belongs together
- the behavior belongs with the data
- naming the concept makes the code clearer
Objects help us express ideas,
not just store values.
Summary
This page is part of a gentle introduction to modern PHP.
Objects are not about complexity — they are about clarity.
When our data starts to feel like a “thing”, give it a name and let it carry the
small actions that belong to it.
Tony de Araujo —New York
