PHPClasses & Objects

Classes & Objects

A class is a blueprint. It describes what data something will hold and what it can do, but it is not, by itself, a real thing you can use — no product exists just because the Product class is defined. An object is what you get when you build a concrete instance from that blueprint with the new keyword. You can create as many objects as you like from one class, and each one is independent of the others.

Declaring a class

The class keyword starts a class definition, followed by the class name and a body in curly braces. Inside the body you declare properties (variables that belong to the class) and methods (functions that belong to the class).

A minimal Product class

PHP
<?php
class Product
{
    public string $name;
    public float $price;

    public function describe(): string
    {
        return $this->name . ' costs $' . $this->price;
    }
}

By itself, this code defines what a Product looks like — it does not create one. Class names are conventionally written in PascalCase, which is how you will recognize them at a glance throughout this course.

Instantiating with new

The new keyword builds an actual object from the class blueprint. The result is stored in a variable just like any other value, and -> (the "arrow" operator) is used to reach a property or call a method on that object.

Creating and using a Product object

PHP
<?php
$product = new Product();
$product->name = 'Wireless Mouse';
$product->price = 25.00;

echo $product->describe();
Wireless Mouse costs $25
What $this refers to

Inside describe(), $this refers to the specific object the method was called on — not the class in the abstract, and not some global product. When you write $product->describe(), PHP runs the method with $this bound to $product. If you had a second object, calling the same method on it would bind $this to that second object instead.

Independent state: two objects, two prices

Each object created from a class gets its own copy of the class's properties. Changing one object's data never affects another object of the same class — they only share the blueprint, not the data.

Two independent Product instances

PHP
<?php
$mouse = new Product();
$mouse->name = 'Wireless Mouse';
$mouse->price = 25.00;

$keyboard = new Product();
$keyboard->name = 'Mechanical Keyboard';
$keyboard->price = 89.00;

echo $mouse->describe() . "\n";
echo $keyboard->describe() . "\n";

$mouse->price = 19.99; // only affects $mouse

echo $mouse->describe() . "\n";
echo $keyboard->describe() . "\n";
Wireless Mouse costs $25
Mechanical Keyboard costs $89
Wireless Mouse costs $19.99
Mechanical Keyboard costs $89

Discounting $mouse had no effect on $keyboard. $mouse and $keyboard are separate objects living at separate places in memory; the Product class only describes their shape.

Checking an object's type with instanceof

Sometimes you receive a variable and need to confirm what class it was built from before working with it — especially useful once inheritance enters the picture and a variable could hold several related types. The instanceof operator checks this and returns a boolean.

instanceof checks the object's class

PHP
<?php
$mouse = new Product();
$notAProduct = 'just a string';

var_dump($mouse instanceof Product);
var_dump($notAProduct instanceof Product);
bool(true)
bool(false)
new always creates a fresh, empty object
`new Product()` does not copy default values from anywhere unless the class itself sets them (typed properties without a default, like the ones above, start uninitialized). Reading an uninitialized typed property before assigning it throws an `Error`. The next page, Properties & Methods, covers giving properties sensible defaults so this never bites you.
  • A class is a blueprint; an object is a concrete instance built from it with new.

  • -> accesses a property or calls a method on a specific object.

  • $this inside a method always refers to the object the method was called on.

  • Every object holds its own copy of the class's properties — changing one object never changes another.

  • instanceof checks whether a variable holds an object built from a given class.

Objects vs. arrays
A `Product` object and a `$product` array holding the same `name`/`price` values may look similar, but only the object can carry behavior (`describe()`) alongside its data, and only the object gives you `instanceof` to verify its shape at runtime.
Tip
When you find yourself creating an object and then immediately setting several properties by hand, as in the examples above, that is usually a sign you want a constructor instead — covered next, in Constructors & Destructors. It guarantees every object starts fully and correctly initialized.