Static methods and properties

1194 0

In certain cases, we might want to approach methods and properties of a class without the need to first create an object out of the class. This can be achieved by defining the methods and properties of a class as static. Even though the use of static methods and properties is not considered a good practice, there are cases in which their use is quite handy and justified.

We can access static methods and properties without the need to first create an object, but their use should be limited.

We have already learned about the three access modifiers called public, protected and private. This chapter is devoted to the fourth modifier, static, that allows access to classes’ properties and methods without the need to create objects out of the classes.

How to define methods and properties as static?

In order to define methods and properties as static, we use the reserved keyword static. In the following example, the class Utilis has a static public property with the name of $numCars, and so the property’s name is preceded by both the static as well as the public keywords.

How to approach static methods and properties?

In order to approach static methods and properties we use the scope resolution operator (::). In the example given below, in order to work with the static property of $numCars, we use the following code:

Result:
3
Pay attention to the use of the $ sign right after the scope resolution operator.

How to approach the static methods from within the class?

In the same manner that we used the $this keyword to approach the class’s own properties and methods from within the class, we use the reserved keyword self to approach static methods and properties.

In the example given below, the method addToNumCars() gets and sets the number of cars from the static $numCars property within the same class by using the self keyword.

Result:
0
3
2

When to use static properties and methods?

The main cases in which we consider the use of static methods and properties are for utilities.

The use of static properties and methods is not a good practice. However, in some cases, the ability to use a property or a method without the need to first create an object out of a class can be advantageous. The main cases in which we consider the use of static methods and properties are when we need them as counters and for utility classes.

Use case 1: as counters
We use static properties as counters since they are able to save the last value that has been assigned to them. For example, the method add1ToCars() adds 1 to the $numberOfCars property each time the method is called.

Result:
0
1
2
3

Use case 2: for utility classes

It is very common to use static methods for utility classes. The sole purpose of utility classes is to provide services to the main classes. Utility methods can perform all kinds of tasks, such as: conversion between measurement systems (kilograms to pounds), data encryption, sanitation, and any other task that is not more than a service for the main classes in our application.

The example given below is of a static method with the name of redirect that redirects the user to the URL that we pass to it as an argument

In order to use the redirect() method, all we need to do is call it from the scope of the Utilis class without having the need to create an object. It’s as simple as this:

Utilis :: redirect(“http://phpgurukul.com/”);

Why static should be used with caution?

Whenever you use static, be sure to use it for utilities and not for convenience reasons.

If you are considering the use of static methods and properties because they are convenient and can be approached without the need to first create an object, please be cautious, because static methods suffer from the following two major disadvantages:

  • Static methods and properties present globals to the code that can be approached from anywhere, and globals are something that we should avoid as much as possible.
  • You will have a hard time performing automated testing on classes that use static methods.

So, whenever you use static, be sure to use it for utilities and not for convenience reasons.

In this article

Join the Conversation


Share