Home > Backend Development > PHP Tutorial > Static Method in PHP

Static Method in PHP

WBOY
Release: 2024-08-29 13:06:45
Original
1022 people have browsed it

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.

How Static Method Works in PHP and its Syntax?

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 Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

public static function static_methodname()
{
//Statements//
}
Copy after login

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;
Copy after login

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();
Copy after login

Similarly to access the static property outside the class, we have to use:: operator.

Syntax:

Class_name::static_property_name;
Copy after login

Example to Implement Static Method in PHP

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>
Copy after login

Output:

Static Method in PHP

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.

Advantages of Static Method in PHP

There are a few advantages to which a static method can be used. They are as follows:

  • It allows many classes to access to the behavior that is not dependent on an instance of any static method.
  • Grouping together stateless utility methods in a helper class makes it very clear of what’s happening and creates a class that is cohesive and coherent.
  • The static method can be used or accessed by using the class name and no object is required.
  • Static methods and properties have a single value applies to all the instances of the program.

Disadvantages of Static Method in PHP

  • Static members should be created with a particular intention because it requires a proper vision of our requirements.
  • Static methods are difficult when the testing has to be done for a particular program as the method and its attributes are globally defined and can be used anywhere in the program. So, the value of the method or property will be set by default and can be a drawback for the implementation.

Rules and Regulations for Defining Static Method in PHP

  • The first and main point of defining the static method is to include static keyword before the method name or the property name that has to be initialized. If the method or property name is not declared as static then the scope of the function/method will not be globally accessed by other methods.
  • The second point is to use Scope Resolution Operator (::) when the static method and property has to be called outside the class. With the scope resolution operator, we have to mention the class name along with the static method or property name.

Conclusion

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.

Recommended Article

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 –

  1. Abstract Class in PHP
  2. Socket Programming in PHP
  3. PHP Frameworks
  4. Public Function in PHP

The above is the detailed content of Static Method in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template