Self Keyword

Gazing into the Mirror of ‘self’ for Code Clarity

This is how most books are built. Let’s use some keywords without explaining it to you previously. I was caught in that trap as well. We looked at the self keyword in the last couple of articles without actually going into detail as to what it means.

https://www.dinocajic.com/php-static-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. The self keyword does not require a dollar sign in front of it, unlike the $this keyword. The is pushed towards the static property, self::$property, whereas for non-static access with the $this keyword, the dollar sign is stripped from the property name: $this->property.

To access a non-static property or method, you append the object operator to $this keyword. To access static properties or methods inside the class, you use the self keyword followed by the scope resolution, or the double-colon, operator.

Static properties and methods can be returned from both static and non-static methods.

Recap. Take a look at the following class that we’ve covered in the previous articles: GermanShepherd

We’ve look at self in our GermanShepherd class as it currently stands.

<?php
require_once("Dog.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++;
    }
  
    //...
}

What we’ve done here is creating a static property called $num_of_dogs_created and below that a static method called update_num_of_dogs_created(). These are both class members and to access them you do not have to instantiate the GermanShepherd object.

To test our example, we’ll create a new file and add the same code we did in the previous article.

<?php

require_once("GermanShepherd.php");
$gs_dog = new GermanShepherd("Brown", "10/10/2019", "Brown");

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

GermanShepherd::update_num_of_dogs_created();
echo "Update it again: " . GermanShepherd::$num_of_dogs_created . "<br>";

Our GermanShepherd class is included and instantiated. In order to call the static members, we do not need to instantiate it, but once we do, we have a call within our constructor that updates the number of dogs created.

<?php
require_once("Dog.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();
    }
  
    //...
}

To access the update_num_of_dogs_created() from our constructor, we have to use the self keyword since it is a static method. We can’t use $this. We have to use self followed by the scope-resolution operator, and finally by the static method name.

The static method increments the static property $num_of_dogs_created, again by appending self to the property name: self::$num_of_dogs_created++;

Let’s create one more non-static method: get_num_of_dogs_created_plus_two(). It’s just something random; don’t read too much into it :). All this method will do is, you guessed it, return the number of dogs created and add 2 to it.

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

class GermanShepherd extends Dog
{

    static int $num_of_dogs_created = 0;

    //...

    public function get_num_of_dogs_created_plus_two() {
        return 2 + self::$num_of_dogs_created;
    }

    //...
}

This is just to illustrate that you can call a non-static method that returns a static value. Let’s call it and see what we get.

The output should look like the following:

Number of instances created: 1
Update it again: 2
Number of dogs create + 2: 4

Walking through the code:

  • We’re calling a static property, $num_of_dogs_created, which returns 1.
  • We’re then calling a static method, update_num_of_dogs_created(), which increments the static property to 2.
  • The static property, $num_of_dogs_created is called again to display 2.
  • Finally, we’re calling an instance method, get_num_of_dogs_created_plus_two() that returns the value of the static property $num_of_dogs_created incremented by 2.

That’s all there is when it comes to the self keyword.

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.

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.

Abstract Classes

BUILDING WORLDS ON ABSTRACT CLASS PILLARS

PHP – P60:ABSTRACT CLASSES

Abstract classes are classes that start with the word abstract. If a class contains at least one abstract method, it must be declared as abstract. If a method inside of an abstract class is defined as abstract, you can only declare the method signature.

Leave a Reply