PHP Static Properties

PHP Static Properties

Just like static methods, Static Properties can be accessed directly without creating an instance (object) of a class.

A static property belongs to the class itself, not to any individual object. If you change the value of a static property, it changes for the entire program!


Defining and Accessing Static Properties

Static properties are declared with the static keyword. To access a static property, use the Class Name, double colon (::), and the property name (with the $ symbol).

Static Property Example

<?php
class Pi {
  public static $value = 3.14159;
}

// Get static property directly echo Pi::$value; ?>

Accessing from Inside

Just like static methods, if you need to access a static property from inside a method in the same class, use the self keyword followed by the scope resolution operator: self::$value.

(Note: Because static properties are not tied to an object, they are heavily used to count the total number of objects created, or to hold global configuration settings).