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
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 $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 $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 $mouse = new Product(); $notAProduct = 'just a string'; var_dump($mouse instanceof Product); var_dump($notAProduct instanceof Product);
bool(true) bool(false)
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.$thisinside 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.
instanceofchecks whether a variable holds an object built from a given class.