Backend Development 8 min read

Understanding the static Keyword in PHP: Properties, Methods, and Use Cases

This article explains PHP's static keyword, covering static properties and methods, memory management, common use cases such as utility functions and the singleton pattern, and important considerations for visibility and inheritance, accompanied by clear code examples.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Understanding the static Keyword in PHP: Properties, Methods, and Use Cases

PHP is an object‑oriented language that provides the static keyword for defining class‑level properties and methods. This article introduces how to use static in classes and methods, and presents practical code examples.

1. Static properties

Static properties belong to the class itself rather than to any instance, allowing shared values across objects. They are declared with the static keyword:

<code><span><span><span>class</span> <span>MyClass</span> </span>{</span>
<span>    <span>public</span> <span>static</span> $myStaticProperty = <span>'Hello, World!'</span>;</span>
<span>}</span>
<span>// Access static property</span>
<span>echo MyClass::$myStaticProperty;</span></code>

Access does not require instantiating the class; the class name is used directly.

2. Static methods

Static methods also belong to the class and can be called without an object. They are useful for operations that do not depend on instance state:

<code><span><span><span>class</span> <span>MyClass</span> </span>{</span>
<span>    <span>public</span> <span>static</span> <span>function</span> <span>myStaticMethod</span>() {</span>
<span>        <span>return</span> <span>'Hello, World!'</span>;</span>
<span>    }</span>
<span>}</span>
<span>// Call static method</span>
<span>echo MyClass::myStaticMethod();</span></code>

Like static properties, static methods are invoked via the class name.

3. Memory management of static variables

Static variables persist for the entire script execution, retaining their values across all instances. Example:

<code><span><span><span>class</span> <span>MyClass</span> </span>{</span>
<span>    <span>public</span> <span>static</span> $count = <span>0</span>;</span>
<span>    <span>public</span> <span>function</span> __construct() {</span>
<span>        self::$count++;</span>
<span>    }</span>
<span>    <span>public</span> <span>function</span> getCount() {</span>
<span>        <span>return</span> self::$count;</span>
<span>    }</span>
<span>}</span>
<span>$obj1 = new MyClass();</span>
<span>$obj2 = new MyClass();</span>
<span>echo $obj1->getCount(); // 2</span>
<span>echo $obj2->getCount(); // 2</span></code>

The static $count property is shared, so both objects report the same count.

4. Common uses of static methods

Utility methods : static methods can provide reusable functionality, such as a simple sum function:

<code><span><span><span>class</span> <span>MathUtils</span> </span>{</span>
<span>    <span>public</span> <span>static</span> <span>function</span> sum($a, $b) {</span>
<span>        <span>return</span> $a + $b;</span>
<span>    }</span>
<span>}</span>
<span>echo MathUtils::sum(5, 3); // 8</span></code>

Singleton pattern : ensures a class has only one global instance, accessed via a static method:

<code><span><span><span>class</span> <span>Singleton</span> </span>{</span>
<span>    <span>private</span> <span>static</span> $instance = <span>null</span>;</span>
<span>    <span>private</span> <span>function</span> __construct() {}</span>
<span>    <span>public</span> <span>static</span> <span>function</span> getInstance() {</span>
<span>        if (self::$instance == null) {</span>
<span>            self::$instance = new Singleton();</span>
<span>        }</span>
<span>        return self::$instance;</span>
<span>    }</span>
<span>}</span>
<span>$singleton1 = Singleton::getInstance();</span>
<span>$singleton2 = Singleton::getInstance();</span></code>

The static getInstance method returns the same object each time, useful for managing shared resources.

5. Important considerations

Static members cannot directly access non‑static members; an instance is required for that.

Visibility of static properties and methods can be public , protected , or private ; choose appropriately to control access.

When a subclass redeclares a static member, it does not override the parent’s member but creates a separate one.

Conclusion

The static keyword in PHP enables class‑level data and behavior, facilitating shared state, utility functions, and design patterns like singletons. Proper use of static members improves code efficiency and maintainability, while awareness of memory and visibility rules prevents common pitfalls.

backendDesign PatternsphpoopSingletonstatic
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.