Explain Static Variable And Function In Php

Created By: Expert
Posted on: 11 months ago

Static is a PHP keyword with many usages. It is almost universally used, though there are many variations of it. Let’s review all the five of them :

  • static method
  • static property
  • static closure
  • static variable
  • static as a classname

Static method

The most common usage of static is for defining static methods.

Such methods are part of a class, just like any method, though they may be used even without any such instantiated object. This is actually the main difference with a normal method : $this is not available in such methods.

  class Foo { 
    public static function aStaticMethod() { 
      $var = 'this'; 
    public function aNonStaticMethod() { 
$object = new foo(); 
$object-> aNonStaticMethod();

Note that static methods may be called from an object, though the pseudo-variable $this will still NOT be available. In fact, PHP emits a fatal error, whenever the $this variable is directly mentioned in a static method : this is why the example has to use the variable variable to actually show the status.

Since static methods do not depends on an object, they are great for factories. They are also useful to access the private static properties, since both of them are static. Finally, they are also useful for utils that are related to a class but not depend on local object state : however, those may be considered as a function.

Static properties

Static properties are the close cousin of static methods. They do not depend on the object, but only on the class itself. They may hold some important information for the class, but not for the objects.

Static properties have several niche usages. The first of them is being a property for a static method. That makes sense : anything a static method has to store must be in a static property. Actually, a private one, or a protected.

They play the role of constants when the value of the constant is dynamically loaded, and thus, are not known at compile time. Non-class constants may be defined at execution time with define, but not class constants : thus, the usage of static properties. They also replace constants when the constant has to be a resource or an array or another object.

They are used when implementing a singleton, when the object instantiation has a prerequisite, or to share data between objects, such as a counter. They may also be used for local cache.

  class Foo { 
    private $total = 0; 
    private $count = 0; 
    // getter for current count 
    public static function getCount() { 
      return self::$count; 
   // increment count and total 
   public function __construct() { 
     ++self::$total; ++self::$count; 
   // decrement count public 
   function __destruct() { 

Static properties tend to be difficult to initialize : all properties may be initialized at coding time (default value), or construct time. Obviously, the latter is not available for static property, and only the default value is possible. The other option is to set the static property in the main code, at bootstrap, leading to some difficult to solve dependencies.

Static variables

Just like there are static methods and static functions, there are both static properties AND static variables.

Static variables are variables that stay within a context, and are available next call.

  function foo() { 
    static $i = 0; 
    echo $i++; 

This actually works like some prehistoric class : the static variable is available to store some cached data : here, we have used it as counter, but static variables makes great cache for any dictionary that shouldn’t be loaded each call. Load the dictionary at the first call, then it will reside in memory for the rest of the execuction.

Note that the variable is completely invisible from the outside world, just like a private property.

Usually, static variables are a first step before making this function a full class : once you understand that testing such unaccessible variable requires some ‘reset’ mechanism, its prehistoric charm wans and the function is refactored as a class.

static as a classname

It is not possible to call a class static but it is well possible to refer to it with this keyword. Within a class body, you may refer to the current class, or any of its parent in case of fallback, using static. That is convenient in a surprising number of situations.

  // This code makes no sense : it is just for illustration. 
   class x { 
      static $p = 1; 
     // for typehinting, static is not possible but self is
     function foo(self $x) { 
    // for instantiation 
    $o = new static( ); 
    // for calling oneself statically 
    static::$x = static::foo(); 
    // for instanceof 
    return $o instanceof static; 

When using static in those situations, static will be dynamically replaced by the current acting class. In the example, it is the x class. When you change the name of the class from x to something else, the rest of the class keeps running : static acts as a relative class name (relative to the code location, that is).

There is another keyword in competition with static : it’s self. Together, they handle the Late Static Bindings.

Basically, self may just be used at the same location, and it will behave almost identically. Except that self is resolved at compile type, ‘statically’ if you pardon the pun, and it will always reference the class it is written in. While static depends on who is calling the static entity.

Confused ?

  // This code makes no sense : it is just for illustration. 
  class x { 
     const NAME = 'X'; 
     static function whoAmI() { 
        print static::NAME.PHP_EOL; 
        print self::NAME.PHP_EOL; 
class y extends x { 
   const NAME = 'Y'; 
// calling as X 
// display X X 
// calling as Y 
// display Y X 


Answerd By: ravi
Answered on: 11 months ago

© 2020 Copyright: IQA