Back to course

Static Properties and Methods

PHP: The Complete 0 to Hero Bootcamp

37. Static Properties and Methods

Normally, class members belong to the object instance. Static members, however, belong to the class itself, not any specific object. They are shared across all instances.

Declaring Static Members

Use the static keyword before the property or method declaration.

Accessing Static Members

  1. From outside the class: Use the double colon (Scope Resolution Operator) ::.
  2. From inside the class: Use self:: instead of $this->.

php

getCount(); // Output: 3 ?>

When to use Static: For utility functions, helper methods, or counters/configurations that should be universal and not dependent on object creation (e.g., mathematical functions, shared config settings).