Static Keyword

Unveiling Timeless Secrets within the Code

Static methods are methods that belong to the class, and not to the object. Methods that belong to objects are sometimes referred to as instance methods. Similarly to how we accessed constants in the previous article without having to instantiate the class, we access static methods without instantiation also.

https://blog.devgenius.io/php-p57-scope-resolution-operator-19c50ca607d4

To access those static methods, we’ll use the class name, followed by the scope resolution operator, and finally the static method name.

ClassName::static_method()

A little Computer Science jargon for you: static methods are stored in a separate portion of the heap memory. Instance methods are stored in stack memory.

Most of the time, a programmer will choose to create static methods and properties from an architectural perspective, such as when a person is trying to implement a Singleton. Don’t worry, we won’t discuss Design Patterns here.

Static methods are created similarly to instance methods with the exception that the static keyword is appended to the declaration.

public static function getNumberOfBankAccount()

Static properties also have the static keyword prepended to them.

static $name;

In our example, we’ll look at how many times an object was instantiated. We don’t want to store the variable outside of our class. If we didn’t use static, each time the object was instantiated, it would read 1. To separate itself from the object, we’ll make it static.

Recap: We have a GermanShepherd class without any static methods or properties.

Let’s start by creating a new file that’s going to test our GermanShepherd class. All we want to do for now is count the number of times that the class was instantiated. In our file, let’s include the GermanShepherd class and create 5 GermanShepherd objects.

<?php
// Static Keyword
require_once("GermanShepherd.php");
$gs_dog0 = new GermanShepherd("Brown", "10/10/2019", "Brown");
$gs_dog1 = new GermanShepherd("Brown", "10/10/2019", "Brown");
$gs_dog2 = new GermanShepherd("Brown", "10/10/2019", "Brown");
$gs_dog3 = new GermanShepherd("Brown", "10/10/2019", "Brown");
$gs_dog4 = new GermanShepherd("Brown", "10/10/2019", "Brown");

Before we continue, we could create a variable inside of the file above and keep record of each time that we instantiate a class. That’s error prone and not great for scalability. Let’s do this the right way and create our static property inside of the GermanShepherd class.

The $num_of_dogs_created static int is created and initialized to 0 inside of the GermanShepherd class.

<?php
require_once("Dog.php");

class GermanShepherd extends Dog
{
    static int $num_of_dogs_created = 0;

    //...
}

Even though we have 5 instantiations, the initialization to 0 only occurs during the creation of the first object. It doesn’t get reset each time.

How do we update the $num_of_dogs_created. You’re right, we could manually update it each time the class is instantiated.

<?php
// Static Keyword
require_once("GermanShepherd.php");
$gs_dog0 = new GermanShepherd("Brown", "10/10/2019", "Brown");
GermanShepherd::$num_of_dogs_created++;
$gs_dog1 = new GermanShepherd("Brown", "10/10/2019", "Brown");
GermanShepherd::$num_of_dogs_created++;
$gs_dog2 = new GermanShepherd("Brown", "10/10/2019", "Brown");
GermanShepherd::$num_of_dogs_created++;
$gs_dog3 = new GermanShepherd("Brown", "10/10/2019", "Brown");
GermanShepherd::$num_of_dogs_created++;
$gs_dog4 = new GermanShepherd("Brown", "10/10/2019", "Brown");
GermanShepherd::$num_of_dogs_created++;

echo GermanShepherd::$num_of_dogs_created;

Will that work? It will. But it’s messy and we haven’t solved anything. It’s almost as bad as having the variable in the file. Let’s not do that.

One option to solve this is to add the increment inside of our constructor.

<?php
require_once("Dog.php");

class GermanShepherd extends Dog
{
    static int $num_of_dogs_created = 0;

    public function __construct(string $eye_color,
                                string $dob,
                                string $fur_color)
    {
        $this->eye_color = $eye_color;
        $this->dob = $dob;
        $this->fur_color = $fur_color;

        GermanShepherd::$num_of_dogs_created++;
    }
  
    //...
}

But, we want to talk about methods as well. What if we wanted to increment the $num_of_dogs_created outside of the class without calling the static property directly. Remember why you would want to use accessors and mutators. We can create a method that updates it and then call that static method.

<?php
class GermanShepherd extends Dog
{

    static int $num_of_dogs_created = 0;

    public static function update_num_of_dogs_created() {
        self::$num_of_dogs_created++;
    }

    public function __construct(string $eye_color,
                                string $dob,
                                string $fur_color)
    {
        $this->eye_color = $eye_color;
        $this->dob = $dob;
        $this->fur_color = $fur_color;

        self::update_num_of_dogs_created();
    }
    
    //...
}

Since we’re calling the static property within the same class, we can use the self keyword to access it from our update_num_of_dogs_created() method. We then call the static method from our constructor. One thing that you might have noticed is that we’re using the dollar sign after the double-colon operator; it’s a little bit different then when using $this keyword where you strip the dollar sign of a property after the object operator.

Let’s give it a shot and see what we get.

<?php
// Static Keyword
require_once("GermanShepherd.php");
$gs_dog0 = new GermanShepherd("Brown", "10/10/2019", "Brown");
$gs_dog1 = new GermanShepherd("Brown", "10/10/2019", "Brown");
$gs_dog2 = new GermanShepherd("Brown", "10/10/2019", "Brown");
$gs_dog3 = new GermanShepherd("Brown", "10/10/2019", "Brown");
$gs_dog4 = new GermanShepherd("Brown", "10/10/2019", "Brown");

echo "Num of instances created: " . GermanShepherd::$num_of_dogs_created . "<br>";

We get Num of instances created: 5, since that was the number of objects that we created.

The method can be called outside of the class to increment it once more if we like.

Walking through the code:

  • We instantiate each GermanShepherd class and increment the $num_of_dogs_created static property each time.
  • The $num_of_dogs_created is echoed out and it displays 5.
  • The update_num_of_dogs_created() method is called again. It increments the $num_of_dogs_created static property by 1.
  • The next echo statement displays 6.

Double Colon

ASCENDING LOGIC PEAKS WITH THE RESOLUTION OPERATOR

PHP – P57:SCOPE RESOLUTION

In PHP, the scope resolution operator, or the double colon ::, allows you to access constants, static properties & methods, and overridden properties & methods.

Static Keyword

Unveiling Timeless Secrets within the Code

PHP – P58:static keyword

Static methods are methods that belong to the class, and not to the object. Methods that belong to objects are sometimes referred to as instance methods.

Self Keyword

GAZING INTO THE MIRROR OF ‘SELF’ FOR CODE CLARITY

PHP – P59:self keyword

For static class members, we don’t use the $this keyword, we use self. That and the scope resolution operator allows us to access the static properties and methods.

Leave a Reply