如何以静态方式访问常量

if a constant is defined in class like this:

class Example
{

   const MIN_VALUE = 0.0;      // RIGHT - Works INSIDE of a class definition.

}

it is possible to access the constant like this:

Example::MIN_VALUE

but if you do this:

class Sample {

    protected $example;
    public function __construct(Example $example){
        $this->example = $example;
    }

    public function dummyAccessToExampleConstant(){

        //doesn't work -> syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM)
        if($this->example::MIN_VALUE === 0.0){

        }

        //this works
        $tmpExample = $this->example;
        if($tmpExample::MIN_VALUE === 1){

        }
    }
}

Can somebody explain me the reason of this behaviour ?

Is there a good reason or is it just a language construct that prevents the access with "::"

Is there a way how to access a constant with "$this"

This is one of those unfortunate shortcomings of PHP's parser. This will work:

$example = $this->example;
$min = $example::MIN_VALUE;

This won't:

$min = $this->example::MIN_VALUE;

Edit:

This issue is documented in PHP bug #63789: https://bugs.php.net/bug.php?id=63789

It has been fixed, but you will have to wait until PHP's next major release (7).

It is a class constant. There is no need (and indeed no means) whatsoever to access it in an instance-based way.

You should just access it as Example::MIN_VALUE to eliminate any confusion.

PHP > 5.3 allows access via an instance as you have shown (i.e. $class_instance::CLASS_CONSTANT) but this is still not to be confused with a property of that instance which can be accessed via -> (if public of course).

Is there a way how to access a constant with "$this"

You don't need to access a constant with $this, because $this refers to the current instanciated object of a class. constants can be accessed without instantiating an object.

Is there a good reason or is it just a language construct ...

A constant, as it's name implies, it's a constant value, meaning the value of that variable won't change during execution, that's why you don't need to instantiate an object to access its value.

Hope it's clear !