How to Create and Access Static Properties and Methods in PHP

php


In this article, we go over how to create and access static properties and methods in PHP.

For example, in a class, we can define properties and methods that are static.

A static method or property is one that can be used without creating an object first.

So, with a regular, non-static method used in a class, you must create an instance (or object) of the class before you can call the method. The method can only be used by being invoked upon an object. This takes the form, object->method().

The same is true for a regular, non-static property or variable. Before the property can be accessed, an object must be created in the class. Only through the object could the property be accessed. This takes the form, object->property.

However, with a static method or property, the method or property is independent of the object.

The static property is a variable that belongs to the class only, not any object. So the property does not belong or is tied to any object.

A static method is a method that is not invoked on any object. It is simply an independent method from any objects. Therefore, when you are creating a static method, inside of it there is no mention of the keyword $this. The keyword $this is used only for non-static methods, since it refers to an object. Since there is no object with static methods, there is no use of the keyword $this.

Static properties are often seen in libraries where the functionality is independent of any object properties. It can be seen as a property separate from the object properties. Static methods, similarly, can be seen as outsiders to objects. Being that they aren't directly invoked on objects, they can be used for outside tasks such as getting the number of objects created in a class, for example.


Static Properties

So now we show how to create static properties inside of a class in PHP.

To create a static property, you add the static keyowrd just before the property name.

Below is an example.



So the above is a static property named $number. We set it equal to 20.

To access a static property, you write the class name, followed by 2 colons (::) followed by the property name (preceded by a $ symbol).

So the code below shows how to access and output the static property $number of the class thisClass.



So now we output the static property $number.


Static Methods

So now we show how to create static methods inside of a class in PHP.

To create a static method, you add the static keyowrd just before the method name.

Below is the general format to do so.



So the above is a static method named move(). Inside of this method is the line echoing, "Move this box".

To call a static method, you write the class name, followed by 2 colons (::) followed by the method name, which includes the parentheses ().

So the code below shows how to call the move() method of the thisClass class.




Full PHP Code

So below is PHP code that creates a static object and a static method. We then output the static property and call the method, so that you can see how to do so.



So in the above code, we create a class named kids.

The first thing we do in the class is create a public static property named $number and set it equla to 20.

Right underneath is we create a public static method named move(). Inside of this move() method, we output the line, "Get up Kids".

We then output the static property $number. This is done through using the class name, followed by 2 colons (::), followed by the property variable. This outputs 20, which is what this property is set to.

We then have a break line just for spacing to make the output more readable.

We then call the move() method by using the class name, followed by 2 colons (::), followed by the method name. This outputs "Get up kids".

Running the above PHP code above yields the following output below.

Actual PHP Output


20
Get up kids


HTML Comment Box is loading comments...