← Back to PHP Course | Chapter 8: OOP | Lesson 10 of 14

PHP Static Methods & Properties

Static means something belongs to the whole class rather than to one object, like the school bell that rings for everyone. You can use it without making an object first.
Syntax
php
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
<?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);
?>

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
<?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);
?>

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
<?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;
?>

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
<?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");
?>

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
<?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");
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using $this inside a static method, where there is no object.
  2. Calling a static method with -> instead of ::.
  3. Forgetting that a static property is shared by every instance of the class.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.