PHP Static Methods and Static Properties
Summary: in this tutorial, you will learn about PHP static methods and static properties. We will show you the differences between
$this and self, and discuss when we can use static methods.Introduction to PHP static methods and static properties
Sometimes, it is useful if we can access methods and properties in the context of a class rather than an object. To do this, you can use
static keyword.
To add a static method to a class, you use the static keyword as follows:
You can put the
static keyword before or after the method’s visibility. However, by convention, the visibility is declared first.
To add a static property to a class, you also use the
static keyword as the following syntax:
The static methods and static properties are not linked to any particular object of the class but the class itself.
To call a static method outside the class, you use the
:: operator as follows:
To access a public static property outside the class, you also use the
:: operator:
However to access static methods and static properties from within an instance of the class, you use
self instead of $this as follows:
Developers, who a new to PHP object-oriented programming, are sometimes confused between
selfand $this. Let’s make the concepts clear.self vs. $this
$this | self |
|---|---|
| Represents an instance of the class or object | Represents a class |
| Always begin with dollar ($) sign | Never begin with dollar($) sign |
| Is followed by the -> operator | Is followed by the :: operator |
| The property name after the -> operator does not take dollar ($) sign, e.g., $this->property. | The property name after the :: operator always take the dollar ($) sign. |
PHP static methods and properties example
The following example illustrates how to use static method and property:
How it works.
- We declared a new class named
Counterthat has two properties:$countand$instance, where$instanceis a static property. Each time a new counter object is created, we increased the value of the$instancestatic property by1. Notice that we accessed the$instanceproperty by using theselfand the::operator. - Besides the regular methods, we declared a static method named
countInstance()that is used to return the number of objects instantiated from theCounterclass. - In the test script section below the class, we created two
counterobjects and displayed the number of instances by calling thecountInstance()static method.
When to define static methods
Before defining a static method, ask yourself a question: “Does it make sense to call the method without instantiating a new object?” . If a logic that can be shared by multiple instances of the class, you can extract the code and put it into a static method.
PHP static methods are often used in utility classes of PHP frameworks. A utility class is a class that contains only static methods.
In this tutorial, we have introduced you PHP static methods and static properties of a class. We then shown you the differences between $this and self , and discussed when we can use static methods.
No comments:
Post a Comment