HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

PHP OOP - Static Properties

Static properties can be called directly - without creating an instance of a class.
Static properties are declared with the static keyword:

Syntax

<?php

class ClassName {
  public static $staticProp = "W3Schools";
}

?>

Output

Example

<?php

class pi {
  public static $value = 3.14159;
}

// Get static property
echo pi::$value;

?>

Output

In this example:

Here, we declare a static property: $value. Then, we echo the value of the static property by using the class name, double colon (::), and the property name (without creating a class first).

PHP - More on Static Properties

A class can have both static and non-static properties. A static property can be accessed from a method in the same class using the self keyword and double colon (::):

Example

<?php

class pi {
  public static $value=3.14159;
  public function staticValue() {
    return self::$value;
  }
}

$pi = new pi();
echo $pi->staticValue();

?>

Output