PHP Static Keyword: Unlocking the Secrets 🎯

beginner
5 min

PHP Static Keyword: Unlocking the Secrets 🎯

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.

What is the 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.

Why use static variables? πŸ“

There are several reasons why you might want to use static variables:

  1. 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.

  2. Class-level data storage: Static variables are a way to store data specific to the class, without having to rely on global variables.

  3. 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!

Creating static variables βœ…

To create a static variable, you simply add the static keyword before the variable declaration within a function. Here's an example:

php
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: 2

In 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.

Using 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:

php
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: 2

In 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.

Quiz Time πŸ“

Quick Quiz
Question 1 of 1

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! πŸš€