Static Properties and Methods
Use static members and understand when to call them without an instance.
Static Properties and Methods is a free PHP Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Introduction: Static Properties and Methods
Static members belong to the class itself rather than to any instance. Access them with ClassName::member or self:: inside the class. They are shared across all instances.
Core Concepts
Static Properties and Methods key concepts:
- Declare with
statickeyword - Access with
::(scope resolution operator) self::inside the class;ClassName::from outside- Late static binding with
static::
Static Property
Example:
<?php
class AppConfig {
private static array $settings = [];
public static function set(string $key, mixed $val): void {
self::$settings[$key] = $val;
}
public static function get(string $key, mixed $default = null): mixed {
return self::$settings[$key] ?? $default;
}
}
AppConfig::set('debug', true);
echo AppConfig::get('debug') ? 'debug on' : 'debug off';Static Method
Continued:
<?php
class MathHelper {
public static function clamp(float $v, float $min, float $max): float {
return max($min, min($max, $v));
}
public static function lerp(float $a, float $b, float $t): float {
return $a + ($b - $a) * $t;
}
}
echo MathHelper::clamp(150, 0, 100); // 100
echo MathHelper::lerp(0, 100, 0.5); // 50Singleton Pattern
Practice:
<?php
class Database {
private static ?Database $instance = null;
private function __construct() {
echo 'Connected to DB';
}
public static function getInstance(): self {
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
}
$db1 = Database::getInstance(); // 'Connected to DB'
$db2 = Database::getInstance(); // no output — same instance
echo ($db1 === $db2) ? 'same' : 'different'; // sameStatic Counter
Deeper:
<?php
class EventEmitter {
private static int $instanceCount = 0;
public function __construct() {
self::$instanceCount++;
}
public static function count(): int {
return self::$instanceCount;
}
}
new EventEmitter();
new EventEmitter();
new EventEmitter();
echo EventEmitter::count(); // 3Late Static Binding
Advanced usage:
<?php
class ParentClass {
public static function create(): static {
return new static(); // resolves to calling subclass
}
public function getClass(): string {
return static::class; // late static binding
}
}
class ChildClass extends ParentClass {}
$obj = ChildClass::create();
echo $obj->getClass(); // ChildClassStatic Factory
Real-world pattern:
<?php
class Response {
private function __construct(
private int $status,
private mixed $body
) {}
public static function ok(mixed $data): self {
return new self(200, $data);
}
public static function notFound(): self {
return new self(404, ['error' => 'Not Found']);
}
public function toJson(): string {
return json_encode(['status' => $this->status, 'body' => $this->body]);
}
}
echo Response::ok(['users' => []])->toJson();Static vs Instance Tradeoffs
Best practices:
<?php
// Static: no object needed, but harder to test/mock
class Validator {
public static function isEmail(string $s): bool {
return (bool) filter_var($s, FILTER_VALIDATE_EMAIL);
}
}
// Instance: injectable dependency, testable
class ValidatorService {
public function isEmail(string $s): bool {
return (bool) filter_var($s, FILTER_VALIDATE_EMAIL);
}
}
// Prefer instance classes in larger applicationsPractical Pattern
Putting it all together with a practical pattern for Static Properties and Methods.
Common Pitfalls
Avoid these common mistakes with Static Properties and Methods:
- Static members are shared across all instances
- Prefer late static binding (static::) over self:: in inheritable classes
- Singletons are a common but sometimes overused pattern
- Static methods are harder to mock in unit tests
Quick Check
What keyword accesses a static property inside the class, respecting inheritance (late static binding)?
Recap: Static Properties and Methods
You've covered Static Properties and Methods. Key points to remember:
- Static members are shared across all instances
- Prefer late static binding (static::) over self:: in inheritable classes
- Singletons are a common but sometimes overused pattern
- Static methods are harder to mock in unit tests
Frequently asked questions
Is the “Static Properties and Methods” lesson free?
Yes — the full text of “Static Properties and Methods” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Static Properties and Methods”?
Use static members and understand when to call them without an instance. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Static Properties and Methods” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Classes, Properties, and Methods
- Constructors and Destructors
- Static Properties and Methods
- Magic Methods Overview