In PHP, static methods are used so that the developer can use the properties and attributes of a static class in the program anywhere needed. To define a static method ‘static’ keyword is used. This keyword is used for the attributes and methods that have to be used in common between the functions and objects in the program. Thus, any programming logic that has to be shared with the functions present in the program has to be written in the static method.
In the above introduction, we learned why are we using static methods in PHP, but we need to know how to use it in PHP programming language. If we can access the methods and properties of a class rather than an object and to achieve this we can use static keyword. To define a particular method as static we need to use a static keyword before the function name.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
public static function static_methodname() { //Statements// }
In the above syntax, the static method is initialized and defined. To add a static property in the respective program we need to use a static keyword before the property name
Syntax:
private static static_propertyname;
In the above syntax, the static property is used with the private keyword/access specifiers and can be used within the class. Whenever we use static, we should be sure to use it for utilities and not for convenience reasons.
A static method will not allow you to define explicit dependencies and also includes global variables in the program that can be accessed from anywhere and anytime whenever needed. It’s difficult to perform automation testing on classes that contain static methods. Most commonly static methods are used for utility classes. The purpose of utility classes is to provide all kinds of services to the main classes. All the utility methods can perform various tasks such as data encryption and decryption, conversion between measurements and other tasks that are not more than any other service for the main class in the program.
If the static method and properties have to be considered because of the thought that they are convenient and easy to use without creating the object, but the static method has its disadvantages: You will feel a hard time while performing automated testing on classes that uses static methods. Static methods and attributes are global and can be used anywhere in the program. This approach is useful when the methods have to be used but has to be avoided as much as possible.
To call a static method outside the class, we have to use:: operator.
Syntax:
Class_name::static_method_name();
Similarly to access the static property outside the class, we have to use:: operator.
Syntax:
Class_name::static_property_name;
Here is an example of how to implement a static method in PHP for an employee class.
Code:
<!DOCTYPE html> <html> <body> <?php class employee { public static function ename() { echo " Employee name is Geeta"; } public function __construct() { self::ename(); } } employee::ename(); new employee(); ?> </body> </html>
Output:
In the above example, the employee class is the main class and the name is the static class that has to be used inside the main class. Here, if the static method has to be called inside the class where it is defined we have to use self-keyword to call the static method whereas if the static method has to be called outside the class it has to be called along with the class name.
There are a few advantages to which a static method can be used. They are as follows:
In this article, we understood how static methods have to be declared and its usage. Also, I understood how it works and various advantages and disadvantages along with how to call the static method and property outside the class using a scope resolution operator. The developer has to understand the fact of the requirement and has to use a static method as it affects the testing part of the programs.
This is a guide to the Static Method in PHP. Here we discuss how the static method works in PHP, examples, advantages, disadvantages along with Rules and Regulations. You can also go through our other suggested articles to learn more –
The above is the detailed content of Static Method in PHP. For more information, please follow other related articles on the PHP Chinese website!