PHP static variable and static function

Spread the love

PHP static variable and static function

What is Static?

PHP static variable and static function: Static meaning is which doesn’t change or remains the same.

In object oriented PHP sometimes its better to use variables and function of a class without declaring an object of that class.

Using static functionality you can achieve this type of access where you need not create an object of a class to use its variables and functions outside of a class. Though such objectless access is considered as bad practice in programming sometimes it is useful.

Such objectless classes are mainly certain utility classes which provide some common functionalities which can be used anywhere within a program.

How to declare a PHP static variable and static function?

You can declare static variables and functions by placing a “static” keyword before them.


Let’s see an example of a PHP static variable and static function.


<?php
 class VisitCounter
 {
  //Static variable declaration
  public static $count;
  
  //Static function declaration
  public static function showCout()
  {
   echo "Inside a static method showCount";
  }
 }
 
 
?>

In the above example, I have declared a variable $count and a function showCount as static by placing “static” keyword before them.

Accessing a PHP static variable and static function outside of a class

To access static variable and function outside of a class without declaring an object of that class you need to use “scope resolution operator” (::). 

Scope resolution operator is just colons placed one after another or double colons.

Using a class name with scope resolution operator and the variable and function name you can access them outside of a class.

Like:

ClassName::$variableName
ClassName::functionName


Example:


//Accessing $count property using class name and scope resolution operator
VisitCounter::$count = 1; 
 
echo "Current count value = ".VisitCounter::$count;

//Accessing showCout method using class name and scope resolution operator
VisitCounter::showCout();



The output will be:


Current count value = 1
Inside a static method showCount

While accessing a property/variable you need to use the variable name with a $ sign before a variable name and after scope resolution operator.

A function can be accessed by using function name with circular braces after scope resolution operator.

Accessing a PHP static variable inside a static function

You can access class’ own static variables inside class’ static function using another keyword which is “self” with scope resolution operator, like “self::”.


Example:  self::$propertyName

Let’s see the below program.

<?php
 class VisitCounter
 {
  public static $count;
  
  public static function showCout()
  {
                        //Using static property inside a static method using self::
   
                         echo "Current value of count = ".self::$count;
   
  }
 }
 
 VisitCounter::$count = 1; 
 
 VisitCounter::showCout();
 
?>



The output will be:


Current value of count = 1

In this way you can access any static class property inside its own method using “self::”, just like in the case of non-static properties you use $this->.

Type of operations where you can use static?

Visitor Counter

As a static variable can save or keep the value which is assigned in the previous call, it can be used as a visitor counter.

You can create a counter which can count the number of visitor on a website, by increasing a static variable by one each time the site is visited.


For example:


<?php
 class VisitCounter
 {
  public static $count = 0;
  
  public static function showCout()
  {
   self::$count++; 
  }
 }
 
 echo "<br>Current count = ".VisitCounter::$count; 
 VisitCounter::showCout();
 
 echo "<br>Current count = ".VisitCounter::$count; 
 VisitCounter::showCout();
 
 echo "<br>Current count = ".VisitCounter::$count; 
 VisitCounter::showCout();
 
 
 
?>



The output will be:


Current visitor count = 0
Current visitor count = 1
Current visitor count = 2

In the above example let’s assume whenever a visitor visits the site showCout() function gets called, where we are incrementing a static property $count by one.

And in each call, the count is increased by one.

Let’s see where can we use a static function.

You can create a utility class whereby defining static functions you can directly use them using a class name.

Utility classes are those classes which can be used anywhere in the program for a specific type of operations, like say “Calculator” class where you can define basic math operations like addition, subtraction, multiplication, and division of two numbers.


Let’s see the same example:


<?php
 class Calculator
 {
  public static function add($n1, $n2)
  {
   return $n1 + $n2;
  }
  public static function subtract($n1, $n2)
  {
   return $n1 - $n2;
  }
  public static function multiply($n1, $n2)
  {
   return $n1 * $n2;
  }
  public static function divide($n1, $n2)
  {
   return $n1 / $n2;
  }
 }
 
 
 echo "Addition of 1 and 2 = ".Calculator::add(1,2);
 
 echo "<br>Subtraction of 2 from 4 = ".Calculator::subtract(4,2);
 
 echo "<br>Multiplication of 3 and 5 = ".Calculator::multiply(3,5);
 
 echo "<br>Division of 10 by 2 = ".Calculator::divide(10,2);

?>



The output will be:


Addition of 1 and 2 = 3
Subtraction of 2 from 4 = 2
Multiplication of 3 and 5 = 15
Division of 10 by 2 = 5

In the above example, I have created four static functions add, subtract, multiply, and divide which are accepting two arguments and performing basic mathematic operations.

Then I have called all the static methods using a class name and a scope resolution operator from outside the class. In a similar way, you can create your own utility classes using static variables and static methods.

We will see one more most important feature introduced in PHP version 5.3 that is Late static binding in the next tutorial.


Spread the love

Leave a Comment