PHP Static Methods & Properties
In this page:
class ClassName {
public static $property = value;
static function method_name() { }
}
ClassName::method_name();
ClassName::$property;
What are Static Members?
A static method or property, declared with the static keyword, belongs to the class itself rather than to any individual object, meaning it's shared across every instance and can even be called with no instance at all.
Example: What are Static Members?
<?php
// Define the class `MathHelper`
class MathHelper {
// Define the function `square` taking `$n`
static function square($n) {
// Return `$n * $n` from this function
return $n * $n;
}
}
// Print `MathHelper::square(4)` to the output
echo MathHelper::square(4);
?>
Login to try C/C++/Java/PHP code in the editor
Static Methods
You call a static method using the class name and :: (scope resolution operator), like MathHelper::square(4), rather than through an object with ->.
Example: Static Methods
<?php
// Define the class `MathHelper`
class MathHelper {
// Define the function `square` taking `$n`
static function square($n) {
// Return `$n * $n` from this function
return $n * $n;
}
}
// Print `MathHelper::square(5)` to the output
echo MathHelper::square(5);
?>
Login to try C/C++/Java/PHP code in the editor
Static Properties
Static properties retain their value between calls and are shared by all instances of the class, which makes them useful for things like a counter that tracks how many objects of a class have been created so far.
Example: Static Properties
<?php
// Define the class `Counter`
class Counter {
static $count = 0;
// Define the constructor `__construct` with no parameters
function __construct() {
self::$count++;
}
}
new Counter();
new Counter();
// Print `Counter::$count` to the output
echo Counter::$count;
?>
Login to try C/C++/Java/PHP code in the editor
Static parent Calls
Static methods cannot access $this, since there's no specific object instance they're running on — they can only work with static properties or with parameters passed in explicitly.
Example: Static parent Calls
<?php
// Define the class `Formatter`
class Formatter {
static $prefix = "LOG: ";
// Define the function `format` taking `$msg`
static function format($msg) {
// Return `self::$prefix . $msg` from this function
return self::$prefix . $msg;
}
}
// Print `Formatter::format("Started")` to the output
echo Formatter::format("Started");
?>
Login to try C/C++/Java/PHP code in the editor
When to Use Static
Static members are a good fit for utility functions that don't depend on any particular object's state, like a formatting helper, but overusing static state for things that should vary per-object can make code harder to test.
Example: When to Use Static
<?php
// Define the class `StringHelper`
class StringHelper {
// Define the function `slugify` taking `$text`
static function slugify($text) {
// Return `strtolower(str_replace(" ", "-", $text))` from this function
return strtolower(str_replace(" ", "-", $text));
}
}
// Print `StringHelper::slugify("Hello World")` to the output
echo StringHelper::slugify("Hello World");
?>
Login to try C/C++/Java/PHP code in the editor
- Using
$thisinside a static method, where there is no object. - Calling a static method with
->instead of::. - Forgetting that a static property is shared by every instance of the class.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: