static, self::, static:: Late Static Binding)Static methods are class methods that can be invoked directly on the class itself without instantiating an object instance using new. Static methods are referenced using the Scope Resolution Operator :: (e.g. ClassName::methodName()).
ClassName::method(); cannot access $this or instance properties.$object->method(); operates on specific instance state $this.self:: vs static::)self::: Binds to the class where the keyword is written.static::: Binds to the runtime class that initiated the call (Late Static Binding).flowchart TD
A["Static Method Call"] --> B["ClassName::staticMethod()"]
B --> C{"Check Binding Scope Keyword"}
C -- self:: --> D["Resolves to Defining Base Class"]
C -- static:: --> E["Resolves to Runtime Child Subclass (Late Static Binding)"]
<?php
declare(strict_types=1);
class MathUtility
{
// Static Helper Method
public static function add(float $a, float $b): float
{
return $a + $b;
}
}
// Late Static Binding Example
class Model
{
public static function getTableName(): string
{
return 'generic_models';
}
public static function explain(): void
{
echo "self:: returns: " . self::getTableName() . "
";
echo "static:: returns: " . static::getTableName() . "
";
}
}
class User extends Model
{
public static function getTableName(): string
{
return 'users_table';
}
}
echo "Static Calculation: " . MathUtility::add(15.5, 24.5) . "
";
echo "--- Late Static Binding Test ---
";
User::explain();
static:: for Inheritance Factory Patterns: Prefer static:: over self:: when writing extensible base model factories.What is the difference between self:: and static:: in inherited static methods? (static:: uses Late Static Binding to resolve the calling subclass!)
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.