Double Colon

Ascending Logic Peaks with the Resolution Operator

In PHP, the scope resolution operator, or the double colon ::, allows you to access constants, static properties and methods, and overridden properties and methods. We’ve created a couple of constants in our previous articles, but we’ve never actually accessed them.

Method Overriding

If you’re accessing, for example, a constant outside of the class, you would append the double colon operator to the instantiated object, or even the class name. If you’re accessing the constant or overridden property or method inside of the class, you would use either selfparent, or the static keyword. We’ll cover self and parent in this article; we’ll cover static in the next.

Self is used to access the constant inside the class itself and parent is used to access the inherited constant, or overridden property or method.

Recap: We have the following classes:

Each of those classes are their own files. If you need to review them, click on their links above to see the classes.

We’ll start by creating a new file and instating our GermanShepherd class.

<?php
// Scope Resolution Operator - Double Colon ::
require_once("GermanShepherd.php");
$gs_dog = new GermanShepherd("Brown", "Jan 10, 2018", "Black");

Peeking inside of our GermanShepherd class, we can see that it has two constants: HAS_HEART and HAS_TAIL. Actually, the HAS_TAIL constant is located in the Dog class and the HAS_HEART constant is located in the Animal class. Through inheritance, they’re available in the GermanShepherd class.

https://blog.devgenius.io/php-p53-class-inheritance-e743de094a06

https://blog.devgenius.io/php-p54-inheritance-chain-3cb62edcb373

Let’s try to var_dump() our HAS_HEART constant.

<?php
// Scope Resolution Operator - Double Colon ::
require_once("GermanShepherd.php");

$gs_dog = new GermanShepherd("Brown", "Jan 10, 2018", "Black");
var_dump( $gs_dog::HAS_HEART );

Running the application above, we get true.

We don’t have to instantiate the object in order to call it. All we have to do is use the class name, followed by the double colon, and then the constant name. Looking at our Car class, we have three constants: HAS_HEADLIGHTSHAS_TAIL_LIGHTS, and HAS_TURN_SIGNALS.

<?php
require_once('Vehicle.php');

class Car extends Vehicle
{

    const HAS_HEADLIGHTS = true;
    const HAS_TAIL_LIGHTS = true;
    const HAS_TURN_SIGNALS = true;

    //...
}

Let’s call the HAS_HEADLIGHTS constant without instantiating the Car object and see if that’s possible.

<?php
// Scope Resolution Operator - Double Colon ::
require_once("Car.php");

var_dump( Car::HAS_TAIL_LIGHTS );

Dumping the constant above yields the following result: true. That works because constants belong to the class and not to the object.

An example that’s frequently used is with the bank account. You would have a constant named NUMBER_OF_ACCOUNTS inside of your BankAccount class. Each time a bank account is opened, the amount of money that belongs to that Bank Account is saved for that object. That money belongs to the object. The number of accounts is incremented since the bank needs to know how many total bank accounts are there. The NUMBER_OF_ACCOUNTS constant belongs to the class, and not to the object.

Let’s see what other examples we can do with the double colon operator. In the previous article, we covered Method Overriding where we overrode the Dog walk() method in our GermanShepherd class.

<?php
//Dog.php
require_once('Mammal.php');

class Dog extends Mammal
{
    //...
    public function walk(): string {
        return "I'm a walking dog";
    }
    //...
}

//GermanShepherd.php
require_once("Dog.php");

class GermanShepherd extends Dog
{

    //...

    public function walk(): string {
        return "I am a walking German Shepherd";
    }
}

What if we wanted to display “I’m a walking German Shepherd” and whatever is returned from the Dog walk() method? This is where we can use the parent keyword to access the walk() method in the parent class.

First, we’ll change the walk() method to echo out the string, and then return the Dog walk() method output.

<?php
//Dog.php
require_once('Mammal.php');

class Dog extends Mammal
{
    //...
    public function walk(): string {
        return "I'm a walking dog";
    }
    //...
}

//GermanShepherd.php
require_once("Dog.php");

class GermanShepherd extends Dog
{

    //...

    public function walk(): string {
        echo "I am a walking German Shepherd";
        return parent::walk();
    }
}

Running the code gives us the following output: I am a walking German Shepherd I’m a walking dog.

<?php
// Scope Resolution Operator - Double Colon ::
require_once("GermanShepherd.php");

$gs_dog = new GermanShepherd("Brown", "Jan 10, 2018", "Black");
echo $gs_dog->walk();

Walking through the code to make sure that it’s solidified in your head:

  • The GermanShepherd class is instantiated.
  • The $gs_dog->walk() method is called.
  • PHP enters the walk() method in the GermanShepherd class and sees an echo statement. It echo’s out “I am a walking GermanShepherd.”
  • Next, PHP sees the return statement for parent::walk().
  • PHP enters the parent class, Dog, and executes the walk() statement in the Dog class, which returns “I’m a walking Dog.”

You wouldn’t normally do this. It’s just an example to show you that it works.

Self

How do we access constants from inside the class itself? With the self keyword. Let’s say that we wanted to access the HAS_HEART constant inside of our GermanShepherd bark() method. We can display the contents with the self::HAS_HEART call.

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

class GermanShepherd extends Dog
{
    //...

    public function bark()
    {
        echo "Has heart: " . self::HAS_HEART;
        echo "Loud Barking";

    }
}

To see the results, let’s instantiate the GermanShepherd class and call the bark() method.

We’ll get the following output: Has Heart: true. Loud Barking.

Once again, you may have noticed that the HAS_HEART constant does not live inside of the GermanShepherd class; it lives in the Animal class. As long as the proper inheritance chain is in place, GermanShepherd has access to it.

Think of self like a replacement for $this on the constant level. Let’s say that a constant exists inside of our GermanShepherd class. If it did, self would call it there. Parent would skip it and start looking in our parent class.

Method Overriding

BREATHING FRESH LIFE INTO LOGIC WITH METHOD OVERRIDES

PHP – P56:METHOD OVERRIDING

PHP’s object oriented principles allow for both method overloading and method overriding. But what do these terms mean?

 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.

Leave a Reply