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.