PHP Static Properties
Static properties in PHP are properties that belong to the class itself rather than to any individual object. They can be accessed without creating an instance of the class.
Defining a Static Property
<?php
class Counter {
public static $count = 0;
public static function increment() {
self::$count++;
}
}
?>
Explanation: This example defines a class Counter
with a static property $count
and a static method increment
that increases the count.
Accessing a Static Property
<?php
Counter::increment();
echo Counter::$count; // Outputs: 1
Counter::increment();
echo Counter::$count; // Outputs: 2
?>
Explanation: This code demonstrates how to call the static method increment
and access the static property $count
without creating an instance of the Counter
class.