Static Members
Chapter Overview
In this chapter you will learn:
- What static means
- Static properties
- Static methods
- Static constants
- self keyword
- static keyword
- Late Static Binding
- Singleton pattern
- Static factories
- PocketMine examples
- Enterprise examples
- Best practices
- Common mistakes
What Does Static Mean?
Normally:
$player1 =
new Player();
$player2 =
new Player();
Each object gets its own data.
Example:
class Player {
public int $coins = 0;
}
$player1->coins = 100;
$player2->coins = 500;
Output:
100
500
Each object has separate values.
Static Means
Belongs to the CLASS itself
instead of individual objects.
Visualization
Normal Property:
Player Object #1 → coins
Player Object #2 → coins
Player Object #3 → coins
Static Property:
Player Class
↓
onlinePlayers
Shared by everyone.
Why Static Exists
Some things should only exist once.
Examples:
- Application Version
- Online Player Count
- Configuration Cache
- Database Connection Pool
- Singleton Instance
- Utility Methods
Static Property
Syntax:
class Server {
public static int $players = 0;
}
Access:
Server::$players
Notice:
::
This is called:
Scope Resolution Operator
Example
class Player {
public static int $online = 0;
public function __construct() {
self::$online++;
}
}
Usage:
new Player();
new Player();
new Player();
echo Player::$online;
Output:
3
Visualization
Player Class
│
└── online = 3
All objects share it.
Difference
Normal Property
public int $coins;
Each object gets one.
Static Property
public static int $online;
Entire class shares one.
Static Methods
Methods may also be static.
Example:
class Math {
public static function add(
int $a,
int $b
): int {
return $a + $b;
}
}
Usage:
echo Math::add(
5,
10
);
Output:
15
Why Static Methods?
Because creating objects may be unnecessary.
Bad:
$math =
new Math();
$math->add();
Better:
Math::add();
Utility Classes
Example:
class StringUtils {
public static function upper(
string $text
): string {
return strtoupper(
$text
);
}
}
Usage:
StringUtils::upper(
"zyro"
);
Static Constants
Example:
class Server {
public const VERSION =
"1.0.0";
}
Usage:
Server::VERSION
self Keyword
Inside static methods:
self::
refers to current class.
Example:
class Server {
public static int $players = 0;
public static function join() {
self::$players++;
}
}
Usage:
Server::join();
self vs this
$this
Refers to:
Current Object
self
Refers to:
Current Class
Example
Invalid:
public static function test() {
$this->hello();
}
Error:
Using $this when not in object context.
Why?
Because static methods do not belong to objects.
Visualization
Object → $this
Class → self
Static Methods Cannot Access Non-Static Members
Invalid:
class Test {
public int $coins = 0;
public static function hello() {
echo $this->coins;
}
}
Error.
Correct
class Test {
public static int $coins = 0;
public static function hello() {
echo self::$coins;
}
}
Static Factories
Very common in frameworks.
Example:
class User {
public static function create(
string $name
): self {
$user =
new self();
return $user;
}
}
Usage:
User::create(
"Aayan"
);
Named Constructors
Example:
class User {
public static function fromArray(
array $data
): self {
}
}
User::fromArray();
User::fromJson();
User::fromDatabase();
Very common pattern.
Singleton Pattern
One of the biggest uses of static.
What is Singleton?
A class that can only have:
ONE INSTANCE
Example:
class Main {
private static ?Main $instance =
null;
public function __construct() {
self::$instance =
$this;
}
public static function getInstance()
: ?Main {
return self::$instance;
}
}
Usage:
Main::getInstance();
PocketMine Example
Most plugins use:
Main::getInstance();
through:
SingletonTrait
Visualization
Plugin
↓
Singleton Instance
Only one exists.
Late Static Binding
One of PHP's advanced concepts.
Example:
class Animal {
public static function make() {
return new self();
}
}
Child:
class Dog
extends Animal {
}
Usage:
Dog::make();
Returns:
Animal
Not:
Dog
Solution
Use:
static
instead of:
self
Example:
class Animal {
public static function make() {
return new static();
}
}
Now:
Dog::make();
returns:
Dog
self vs static
self
Current class.
static
Runtime class.
Visualization
self
↓
Animal
static
↓
Dog
Example
class Animal {
public static function who() {
echo static::class;
}
}
class Dog
extends Animal {
}
Usage:
Dog::who();
Output:
Dog
Real Framework Examples
Laravel uses:
User::create()
User::query()
User::find()
These rely heavily on static systems.
PocketMine Examples
Server Singleton
Server::getInstance()
Type Converter
TypeConverter::getInstance()
Item Parsers
StringToItemParser::getInstance()
Plugin Main
Main::getInstance()
Utility Examples
UUID Generator
UUID::random();
Configuration Loader
Config::load();
Json Helper
Json::encode();
Benefits
1. No Object Creation
Faster.
2. Shared State
Perfect for:
Counters
Caches
Configurations
3. Cleaner APIs
Example:
User::find();
instead of:
(new User())
->find();
Common Beginner Mistakes
Using Static Everywhere
Static is powerful.
But excessive static usage:
Creates global state.
Tight Coupling
Bad:
Database::query();
Everywhere.
Hard to test.
Hidden Dependencies
Static systems often make code difficult to maintain.
Bad Example
class UserService {
public function save() {
Database::query();
}
}
Cannot easily replace database.
Better
public function __construct(
DatabaseInterface $db
)
Use Dependency Injection.
When To Use Static
Good uses:
✅ Utility Classes
✅ Constants
✅ Singleton Access
✅ Factories
✅ Parsers
When NOT To Use Static
Avoid:
❌ Business Logic
❌ Services
❌ Databases everywhere
❌ Large applications
Static and Memory
Static properties live:
Until script ends.
They remain shared globally.
Exercises
Exercise 1
Create:
Player::$online
Track online players.
Exercise 2
Create:
Math::add()
Math::subtract()
Math::multiply()
Exercise 3
Create:
UUID::generate()
Mini Project
Create:
ZyroNetwork Server Manager
Static Features:
Server Version
Online Count
Maintenance Mode
Configuration Cache
Interview Questions
What is a static property?
A property shared by all objects.
Can static methods use $this?
No.
Difference between self and static?
self → current class
static → runtime class
What is Late Static Binding?
Using runtime class instead of parent class.
What is Singleton?
A class having only one instance.
Summary
Static Members provide:
✅ Shared State
✅ Utility Methods
✅ Singleton Access
✅ Factory Methods
✅ Cleaner APIs
But use carefully.
Too much static code can become difficult to maintain.
References
https://www.php.net/manual/en/language.oop5.static.php
https://www.php.net/manual/en/language.oop5.late-static-bindings.php
Next Chapter
➡ Final Keyword
➡ Magic Methods
➡ Enums
➡ Namespaces