Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referring to a constant in PHP 7 using static::MY_CONST vs self::MY_CONST vs SomeClass::MY_CONST

Tags:

I would like to make it clear once and for all.

I'm pretty sure I know when to use self::MY_CONST and SomeClass::MY_CONST but it's unclear when to use static::MY_CONST.

You use self::MY_CONST…

…when you refer to a constant that is defined in the same class where you call it.

Example:

class Foo 
{
    const MY_CONST = 123;

    public function example() 
    {
        echo self::MY_CONST;
    }
}

You use AnotherClass::MY_CONST…

…when you refer to a constant that is defined in different class that the one from where you call it.

Example:

class Bar
{
    const MY_CONST = 123;
}

class Foo 
{
    public function example() 
    {
        echo Bar::MY_CONST;
    }
}

You use static::MY_CONST…

…when? I don't know. In terms of referring constants using static makes no sense to me. Please provide a valid reason or confirm that self:: and SomeClass:: examples are sufficient.

edit: My question is not a duplicate. I don't ask about $this at all. Don't mark this as a duplicate.

like image 731
Matt Komarnicki Avatar asked Aug 21 '18 10:08

Matt Komarnicki


People also ask

What is the difference between static and constant in PHP?

A static keyword is been used to declare a variable or a method as static. A const keyword is been used to assign a constant or a fixed value to a variable. In JavaScript, the static keyword is used with methods and classes too. In JavaScript, the const keyword is used with arrays and objects too.

How to access constant in PHP class?

To access the class constant, you use the name of the class (in this case mathematics), followed by 2 colons (::), followed by the name of the constant (in this case PI). In this code, we echo out the constant, so an echo precedes this code. Usually by convention, constants are put in all uppercase.

Can a class be constant?

Class Constants ¶ It is possible to define constants on a per-class basis remaining the same and unchangeable. The default visibility of class constants is public .


1 Answers

The static keyword is needed for something called "Late Static Binding" (see also What exactly are late static bindings in PHP?). The manual page on that topic is not the clearest, but this sentence is key:

"Late binding" comes from the fact that static:: will not be resolved using the class where the method is defined but it will rather be computed using runtime information.

Effectively, static:: is similar to $this, in that it resolves to the class where the code is running, which might be a sub-class of the class where the code is written.

Let's use your example of self::, and add a sub-class:

class Foo 
{
    const MY_CONST = 123;

    public function example() 
    {
        echo self::MY_CONST;
    }
}

class Bar extends Foo
{
    const MY_CONST = 456;
}

$bar = new Bar;
$bar->example();

This will output 123, because the self:: in the definition always refers to Foo, regardless of how you call it.

However, if we change to use late static binding:

class Foo 
{
    const MY_CONST = 123;

    public function example() 
    {
        echo static::MY_CONST;
    }
}

class Bar extends Foo
{
    const MY_CONST = 456;
}

$bar = new Bar;
$bar->example();

Now it will echo 456, because the static:: resolves to the class we were actually using at runtime when we made the call, which was Bar, and Bar::MY_CONST has a different value.

like image 99
IMSoP Avatar answered Oct 04 '22 15:10

IMSoP