PHP
static vs instance
Learn the difference between static and instance members and when to use each.
static and instance members
An instance property is private to each instance: $user1->name and $user2->name can hold different values. A static property belongs to the class: User::$count is a single value shared by all instances.
An instance method is called through an instance ($user->save()) and has $this. A static method is called with the class name (User::create()) and has no $this.
Rule of thumb: use instance when you need instance state, static when you do not. Pure helpers like strlen are static; user data is instance.
Advantages and disadvantages of static members
Static members have advantages: no instantiation needed, shared state lives in one place, calls are fast (User::create()). They are handy for grouping helper functions.
But there are disadvantages: static methods are hard to mock for tests; static properties create global state that must be reset between tests; polymorphism does not work.
Advice: prefer instance by default; choose static only when you truly need no instance state or when implementing a known pattern (singleton, registry, factory). Always keep testability as the top criterion.
Exercise BankAccount: static and instance together
Define a BankAccount class with a public static int $totalAccounts = 0 static property and a public string $owner instance property. The constructor takes an $owner parameter, does $this->owner = $owner and increments with self::$totalAccounts++. Add public static function getTotalAccounts(): int that does return self::$totalAccounts. Create two accounts and write echo BankAccount::getTotalAccounts() . "\n".
Show example solution
:$totalAccounts++. Add static function getTotalAccounts(): int { return self::$totalAccounts; }. Call new BankAccount(...) twice, then echo BankAccount::getTotalAccounts() . "\n".
Example solution:
<?php
// BankAccount: static and instance together
class BankAccount {
public static int $totalAccounts = 0;
public string $owner;
public function __construct(string $owner) {
$this->owner = $owner;
self::$totalAccounts++;
}
public static function getTotalAccounts(): int {
return self::$totalAccounts;
}
}
new BankAccount("Alex");
new BankAccount("Sam");
echo BankAccount::getTotalAccounts() . "\n";
Test yourself
-
When should you prefer a static method?
- When the method does not need instance state
- When you need $this
- When the method works on instance-specific data
- When the method is called from the constructor
Show answer
When the method does not need instance state — Correct. Pure helpers that need no instance state are static.
-
Instance property assignment
class User {
public string $name;
public function __construct(string $name) {
$this->name = ____
}
}
Show answer
class User {
public string $name;
public function __construct(string $name) {
$this->name = $name;
}
}
-
Match the static and instance expressions
- $this->name
- Instance property access
- self::$count
- Static property access
- $obj->method()
- Instance method call
- Class::method()
- Static method call
Next lesson: Summary - Static members