A class is a blueprint for objects. class User { ... } declares the type. Create an instance with new User() — that expression returns an object of that class.
PHP Easy · Lesson 1 · On the house
Classes and properties
class, $this, public properties. Lesson 1 is free — on the house.
Lessons · PHP Easy · Lesson 1 of 10 · On the house
Classes and properties
class, $this, public properties.

Properties hold per-object state. public string $name; declares a typed public property (PHP 7.4+ typed properties; PHP 8.x is the Easy target). Assign with $user->name = "Ada"; — the arrow -> reaches into the object.
Inside a method, $this refers to the current instance. $this->name reads or writes that object's property. Outside the class, use the variable that holds the object: $user->name.
Methods are functions declared inside the class: public function greet(): void { echo $this->name; }. Call them with $user->greet();. Visibility defaults matter later; start with public so callers can use the API.
Misses: writing user.name like JavaScript without ->, forgetting $ on $this, treating the class name as an already-built object without new, or assigning undeclared dynamic properties when you meant a declared typed property.
classes
<?php
class User {
public string $name;
public function greet(): void {
echo "hi " . $this->name . "\n";
}
}
$user = new User();
$user->name = "Ada";
$user->greet();Quiz
How do you create an instance of User?
Quiz
What does $this refer to inside a method?
Quiz
How do you read a public property on $user?
Quiz
What keyword declares an object blueprint?
Check
Match the still for: Classes and properties.
Open-book: the answers are on this page. Pass every quiz and check (5) to mark the lesson done. This visit: 0/5.