Static properties and methods

So far, all the properties and methods were linked to a specific instance; so two different instances could have two different values for the same property. PHP allows you to have properties and methods linked to the class itself rather than to the object. These properties and methods are defined with the keyword static.

private static $lastId = 0;

Add the preceding property to the Customer class. This property shows the last ID assigned to a user, and is useful in order to know the ID that should be assigned to a new user. Let's change the constructor of our class as follows:

public function __construct(
    int $id,
    string $name,
    string $surname,
    string $email
) {
    if ($id == null) {
        $this->id = ++self::$lastId;
    } else {
        $this->id = $id;
        if ($id > self::$lastId) {
            self::$lastId = $id;
        }
    }
    $this->name = $name;
    $this->surname = $surname;
    $this->email = $email;
}

Note that when referring to a static property, we do not use the variable $this. Instead, we use self::, which is not tied to any instance but to the class itself. In this last constructor, we have two options. We are either provided with an ID value that is not null, or we send a null in its place. When the received ID is null, we use the static property $lastId to know the last ID used, increase it by one, and assign it to the property $id. If the last ID we inserted was 5, this will update the static property to 6, and then assign it to the instance property. Next time we create a new customer, the $lastId static property will be 6. Instead, if we get a valid ID as part of the arguments, we assign it, and check if the assigned $id is greater than the static $lastId. If it is, we update it. Let's see how we would use this:

$customer1 = new Customer(3, 'John', 'Doe', '[email protected]');
$customer2 = new Customer(null, 'Mary', 'Poppins', '[email protected]');
$customer3 = new Customer(7, 'James', 'Bond', '[email protected]');

In the preceding example, $customer1 specifies that his ID is 3, probably because he is an existing customer and wants to keep the same ID. That sets both his ID and the last static ID to 3. When creating the second customer, we do not specify the ID, so the constructor will take the last ID, increase it by 1, and assign it to the customer. So $customer2 will have the ID 4, and the latest ID will be 4 too. Finally, our secret agent knows what he wants, so he forces the system to have the ID as 7. The latest ID will be updated to 7 too.

Another benefit of static properties and methods is that we do not need an object to use them. You can refer to a static property or method by specifying the name of the class, followed by ::, and the name of the property/method. That is, of course, if the visibility rules allow you to do that, which, in this case, it does not, as the property is private. Let's add a public static method to retrieve the last ID:

public static function getLastId(): int {
    return self::$lastId;
}

You can reference it either using the class name or an existing instance, from anywhere in the code:

Customer::getLastId();
$customer1::getLastId();
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.38.92