PHP Static Members
Static members belong to the class itself, not to instances. They're shared across all objects - perfect for counters, utilities, and configuration!
Output
Click Run to execute your code
Static Properties
<?php
class Counter {
public static $count = 0;
}
// Access without instance
echo Counter::$count; // 0
Counter::$count++;
?>
Static Methods
<?php
class Math {
public static function add($a, $b) {
return $a + $b;
}
}
echo Math::add(5, 3); // 8 - no instance needed!
?>
self:: Keyword
<?php
class Counter {
public static $count = 0;
public static function increment() {
self::$count++; // Access static property
}
}
?>
Summary
- static: Class-level member
- :: Scope resolution operator
- self:: Access static members
- Shared: Same across all instances
What's Next?
Finally, learn about Magic Methods - special methods that PHP calls automatically!
Enjoying these tutorials?