Static methods and properties | PHP Gurukul


 

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.

Click : https://phpgurukul.com/static-methods-and-properties-2/

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: 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.

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.

Blog : https://phpgurukul.com/traits-and-code-inclusion/

PHP Gurukul

Welcome to PHPGurukul. We are a web development team striving our best to provide you with an unusual experience with PHP. Some technologies never fade, and PHP is one of them. From the time it has been introduced, the demand for PHP Projects and PHP developers is growing since 1994. We are here to make your PHP journey more exciting and useful.

Website : https://phpgurukul.com

Comments

Popular posts from this blog

Pre-School Enrollment System using PHP and MySQL | PHP Gurukul

Global Food Ordering System Using PHP and MySQL | PHP Gurukul

Doctor Appointment Management System Using PHP and MySQL | PHP Gurukul