Welcome to today's lesson! Today, we're going to delve into the world of PHP and explore a powerful keyword that every developer should know: the static keyword.
Before we dive in, let's quickly recap what functions and variables are in PHP. Functions are collections of instructions that perform a specific task, while variables are containers that hold data.
static keyword in PHP? π‘In PHP, the static keyword is used to create class variables that belong to the class itself, rather than individual instances of the class. These variables are known as static variables.
static variables? πThere are several reasons why you might want to use static variables:
Persistence across function calls: Since static variables are class-level variables, they are only created once per class. This means that their value persists across multiple function calls, even when different instances of the class are being used.
Class-level data storage: Static variables are a way to store data specific to the class, without having to rely on global variables.
Function caching: By using static variables, you can cache the result of a function, improving performance by avoiding repeated calculations.
Now that we know why we use static variables, let's see how to create them!
static variables β
To create a static variable, you simply add the static keyword before the variable declaration within a function. Here's an example:
class MyClass {
static public $counter = 0;
public function incrementCounter() {
self::$counter++;
}
}
// Creating instances of MyClass
$instance1 = new MyClass();
$instance1->incrementCounter();
$instance2 = new MyClass();
$instance2->incrementCounter();
echo MyClass::$counter; // Outputs: 2In this example, we've created a class called MyClass with a static variable $counter. We've also defined a function incrementCounter() that increases the value of $counter. When we create two instances of MyClass and call incrementCounter() on each, the value of $counter increases by 2, demonstrating that the value persists across function calls.
static functions π‘In addition to static variables, you can also declare functions as static. When a function is declared as static, it cannot access non-static variables or methods within the class, and it can only be called from within the class itself or from outside the class using the scope resolution operator ::. Here's an example:
class MyClass {
static public function getCounter() {
return self::$counter;
}
static private $counter = 0;
}
echo MyClass::getCounter(); // Outputs: 0 (since $counter is private)
$instance = new MyClass();
$instance->incrementCounter();
$instance->incrementCounter();
echo MyClass::getCounter(); // Outputs: 2In this example, we've created a static function getCounter() that returns the value of the static variable $counter. We've also made $counter private, so it can only be accessed through getCounter(). When we call getCounter() before and after increasing the value of $counter, we can see that the value persists.
What is the purpose of the `static` keyword in PHP?
Now that you understand the basics of the static keyword, let's put it into practice! In the next lesson, we'll build a simple PHP application using static variables to store user data. See you there! π