Categories
PHP

Static Variables and Methods

You can use the static keyword to declare class properties and methods. A static member can be accessed without having to create a new instance of the class. The instances of the class get the same value of a static variable because it belongs to the class, not the instance.

Static Member Variables

For situations where you would like to associate some data with an entire class of objects, but the data is not fixed (in other words, constant), you can create member variables that are global to the entire class, not just specific instances. In PHP, these are called static member variables. They are declared by prefixing their declarations with the keyword static.

<?php
 class User {
  public static $x;
 }

You would declare static member variables just like any other; you can specify a visibility modifier (public, private, or protected) with it and assign it an initial value. For the previous example, class User, I have declared a static class variable called x that anybody can access and modify. Any instance of class User sees the same value if it asks for the value of x, and it does not have its own storage location for it.

<?php
 class User {
  public static $x = 10;
  public function reset() {
   self::$x = 0;
  }
 }
 User::$x++;
 echo User::$x . ' '; # Prints: 11
 
 User::$x = 100;
 echo User::$x . ' '; # Prints: 100
 
 $u = new User();
 $u->reset();

Unlike class constants, static member variables are not read-only. Accessing the value of a static variable is similar to accessing a class constant: You either use the type name: User::$x (outside of or within the class) or the keyword self (self::$x) within the class, both followed by the scope resolution operator (::) and the name of the static variable, starting with $.

In the previous example, to use the reset() method you’ve to create a new instance of the User class. You can create a static method if you want to directly access the reset() method.

As a more interesting example, I can use a static member variable to count the number of instances of a class that have been created:

<?php
 class User {
  private static $x = 0;
  protected $username;
  public function __construct ($u = 'Guest') {
   $this->username = $u;
   self::$x++;
   echo 'Instances created: '. self::$x .'<br>';
  }
  public function getUsername() {
   return $this->username;  
  }
 }
 
 $user1 = new User('Admin');
 # Prints: Instances created: 1
 
 $user2 = new User();
 # Prints: Instances created: 2
 
 $user3 = new User('BrainBell');
 # Prints: Instances created: 3

Static Methods

There will also come a time when you want to associate methods with your type that do not necessarily operate on a specific instance but are broad operations related to that type.

For example, I might want to define a method on the User class that returns all instances (objects) of the class. I declare a static array variable $users that stores each instance of the class and I declare a static method getAllInstances() by declaring a normal function with the static keyword included, this method returns the $users array:

<?php
 class User  {
  protected static $users = [];
  protected $username;
  public function __construct($u) {
   $this->username = $u;
   self::$users[$u] = $this; # Store each instance
  }
  public function getUsername() {
   return $this->username;
  }
  public static function getAllInstances() {
   return self::$users;
  }
 }
 
 # The new instances are not assigned to variables
 new User('Admin');
 new User('BrainBell');
 new User('Guest');
 
 $users = User::getAllInstances();
 
 foreach($users as $user)
  echo $user->getUsername() . ', ';
 # Prints: Admin, BrainBell, Guest,

Static methods are allowed to have a visibility modifier of private or protected to restrict access to information.

:: vs. -> and self vs. $this

For people confused about the difference between :: and -> or self and $this, I present the following rules:

  • If the variable or method being referenced is declared as const or static, then you must use the :: operator.
  • If the variable or method being referenced is not declared as const or static, then you must use the -> operator.
  • If you are accessing a const or static variable or method from within a class, then you must use the self-reference self.
  • If you are accessing a variable or method from within a class that is not const or static, then you must use the self-referencing variable $this.

PHP OOP Tutorials: