EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Static Method in PHP

Home » Software Development » Software Development Tutorials » PHP Tutorial » Static Method in PHP

Static Method in PHP

Introduction to Static Method in PHP

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.

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

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,726 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

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;

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>

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

PHP Training (5 Courses, 3 Project)

5 Online Courses

3 Hands-on Project

28+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • PHP strpos
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PHP Training (5 Courses, 3 Project) Learn More