Classes and Objects
Defining a class, creating instances, constructors, and the visibility modifiers that control access.
2 min read
PHP has been a fully object-oriented language for a long time now — modern PHP code, and every major framework, leans heavily on classes.
Defining a class
<?php
class User {
public string $name;
public string $role;
public function greet(): string {
return "Hi, I'm {$this->name}";
}
}
$user = new User();
$user->name = "Amara";
$user->role = "admin";
echo $user->greet();A class is a blueprint; new User() creates an instance (an object) from it. $this inside a method refers to the specific instance the method was called on — $user->name and some other $otherUser->name are independent, even though both come from the same class.
Constructors
<?php
class User {
public string $name;
public string $role;
public function __construct(string $name, string $role = "member") {
$this->name = $name;
$this->role = $role;
}
}
$user = new User("Amara", "admin");__construct() runs automatically when new creates an instance, and is the standard place to require and assign a new object's initial state — this is far more common in real code than setting properties one by one after creation, as in the first example.
Constructor property promotion
<?php
class User {
public function __construct(
public string $name,
public string $role = "member",
) {}
}
$user = new User("Amara", "admin");
echo $user->name;This shorthand (added in PHP 8) declares and assigns a property directly in the constructor's parameter list — public string $name here both defines the $name property and assigns the passed-in value to it, eliminating the repetitive $this->name = $name; lines from the previous example.
Visibility: public, protected, private
<?php
class BankAccount {
private float $balance = 0;
public function deposit(float $amount): void {
$this->balance += $amount;
}
public function getBalance(): float {
return $this->balance;
}
}
$account = new BankAccount();
$account->deposit(100);
echo $account->getBalance(); // 100
// $account->balance; // Error: cannot access private propertypublic properties/methods are accessible from anywhere. private restricts access to code inside the same class only — $account->balance from outside would be a fatal error, forcing all changes to go through deposit(), which can enforce rules (like never allowing a negative balance) that direct property access would bypass entirely. protected sits in between: accessible within the class and any class that extends it, covered in the next lesson.
Static properties and methods
<?php
class Counter {
public static int $count = 0;
public static function increment(): void {
self::$count++;
}
}
Counter::increment();
Counter::increment();
echo Counter::$count; // 2static members belong to the class itself, not to any individual instance — accessed with :: instead of ->, and shared across every instance (or accessible with no instance at all, as shown here). self:: refers to the current class from inside a static context.
Test what you just learned
4 quick questions. Get all of them right to unlock the next lesson.
You can take the quiz without an account — logging in just lets your result count toward your progress.