Static Properties
Last updated onStatic properties let you store data across different objects which helps us to keep the shared information in one place. This keeps your code more cleaner.
In the next sections, you will see why and how to use PHP static properties, let's get started.
What are Static Properties in PHP
Static properties belong to the class, not to individual instances. When a property is declared as static, every instance of the class can access and modify the same piece of data.
So instead of creating a new version of that data for every object, PHP lets all objects in a class "share" one variable. This is great for values you want to keep the same across all instances, like a counter or a default setting.
Here is an example:
class Counter {
public static $count = 0;
public function __construct() {
self::$count++;
}
}
So every time a new
object is created, the shared Counter
property increments, regardless of how many instances there are.$count
Let's take a look at the following section, to understand how to access Static Properties in PHP.
Accessing PHP Static Properties (No Instances Required!)
You do not need to create an instance of the class to access when you use static properties. Instead, you can reach them directly through the class name using the ::
syntax, also called the scope resolution operator.
Here is an example:
class Greeting {
public static $message = "Hello, PHP!";
public static function displayMessage() {
return self::$message;
}
}
// Accessing the static property
echo Greeting::$message; // Outputs: Hello, PHP!
In this case, you can get
directly, without creating an instance of Greeting::$message
. It is like having a single bulletin board that everyone can see and update, rather than giving everyone their board.Greeting
In the following section, you will learn how to apply this concept through examples.
Examples of PHP Static Properties
Consider users logging in and out of your website. So you need to track the number of active users is a good use for static properties because it allows all instances of a class to update a single shared counter.
class User {
public static $activeUsers = 0;
public function __construct() {
self::$activeUsers++;
}
public function __destruct() {
self::$activeUsers--;
}
}
// Create new users
$user1 = new User();
echo User::$activeUsers; // Outputs: 1
$user2 = new User();
echo User::$activeUsers; // Outputs: 2
So, with each new
, the shared User
count goes up. When a user is removed (or the object is destroyed), the count decreases. This method keeps everything in sync without cluttering each instance with individual counters.activeUsers
Another common usage of static properties is in setting defaults that all instances rely on. For instance, if you are building a pricing system, you might want a default tax rate that applies to every item. A static property keeps this setting accessible across all instances of the class.
class Pricing {
public static $defaultTaxRate = 0.07; // 7%
public static function calculateWithTax($amount) {
return $amount * (1 + self::$defaultTaxRate);
}
}
// Access default tax rate
echo Pricing::calculateWithTax(100); // Outputs: 107
The tax rate is consistent across every calculation. If you ever need to adjust the rate, changing it once updates it for all calculations using the
class.Pricing
let's summarize it.
Wrapping Up
PHP static properties allow you to create a single and shared property for all instances of a class. This can be useful when you want a variable to remain consistent across every object created from that class. With static properties, you avoid repetition, and save memory to shared data.
- Static Property: A variable that belongs to a class, not individual instances.
- Scope Resolution Operator (
): The syntax used to access static properties directly through the class name.::
- Optimized Memory Usage: By using static properties, you can prevent unnecessary duplication across instances.
That's it, if you need to read more tutorials in PHP, click here. Thank you for reading.
Frequently Asked Questions (FAQs)
What are 'static properties' in PHP?
How do I declare a static property in PHP?
How can I access a static property in PHP?
When should I use static properties?
How do static properties save memory in PHP?
How can I increment a counter with static properties in PHP?
Can I access static properties inside a class method?
How do static properties help with setting default values?
What is the scope resolution operator in PHP?
What is an example of tracking users with static properties?