18

The PHP manual says

Like static members, constant values can not be accessed from an instance of the object.

which explains why you can't do this

$this->inst = new Classname();
echo $this->inst::someconstant;

but then why does this work?

$this->inst = new Classname();
$inst = $this->inst;
echo $inst::someconstant;
pettazz
  • 593
  • 1
  • 5
  • 16

4 Answers4

22

From the PHP interactive shell:

php > class Foo { const A = 'a!'; }
php > class Bar { public $foo; }
php > $f = new Foo;
php > $b = new Bar;
php > $b->foo = $f;
php > echo $b->foo::A;
PHP Parse error:  syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM, expecting ',' or ';' in php shell code on line 1

The reason that the former syntax fails is that the PHP parser doesn't know how to resolve the double-colon after the property reference. Whether or not this is intentional is unknown.

The latter syntax works because it's not going through the property directly, but through a local variable, which the parser accepts as something it can work with:

php > $inst = $b->foo;
php > echo $inst::A;
a!

(Incidentally, this same restriction is in place for anonymous functions as properties. You can't call them directly using parens, you have to first assign them to another variable and then call them from there. This has been fixed in PHP's trunk, but I don't know if they also fixed the double colon syntax.)

Charles
  • 50,943
  • 13
  • 104
  • 142
  • So it really may just boil down to a bug in the php parser that will be fixed in later versions? – pettazz Mar 27 '11 at 06:23
  • 1
    It's either an oversight or an intentional decision. I'm not sure I'd call it a bug. The PHP internals folks tend to think that people are easily confused, so they try not to let possibly "ambiguous" syntax go through. The double colon is also used to refer to static methods; a static method being called on an instance of a class would be utterly nonsensical. That said, if they were going to restrict *that* behavior, it'd be a runtime error, not a *parse* error. – Charles Mar 27 '11 at 06:27
  • Yes, it's been a decade and nobody should use PHP 5 anymore, but here's an 3v4l based on this example that shows the problem was fixed in PHP 7+: https://3v4l.org/4Vj1N – Mark Hamstra May 29 '21 at 10:39
11

If you are within the class, you can access the constant like this:

self::MY_CONSTANT;

For example:

class MyClass {
    const MY_CONSTANT = 'constant value';

    public function showConstant() {
        echo self::MY_CONSTANT;
    }
}

http://php.net/manual/en/language.oop5.constants.php

Andrew
  • 18,680
  • 13
  • 103
  • 118
  • totally offtopic – woens Jun 28 '21 at 14:16
  • @woens The topic is "Accessing PHP Class Constants" and this answer is about accessing PHP class constants. – Andrew Jun 30 '21 at 13:39
  • 1
    you should use static over self because with the self keyword you might get unwanted side-effects. see https://stackoverflow.com/questions/11710099/what-is-the-difference-between-selfbar-and-staticbar-in-php – Alexander Behling Dec 05 '22 at 14:28
8

To quote the manual:

As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value can not be a keyword (e.g. self, parent and static).

It goes on to use this example:

$class = new MyClass();
echo $class::constant."\n"; // As of PHP 5.3.0

So $inst::someconstant is supposed to work.

As to why $this->inst::someconstant gives a parsing error, I don't know. PHP is funny about some things.

Matthew
  • 47,584
  • 11
  • 86
  • 98
0

php supports accessing class constants from an object instance. Code below is working (checked in phpv5.5.5):

<?php
class superheroes{
    const kal_el = 'Superman';
}

$instance = new superheroes;
echo $instance::kal_el;

Source: http://dwellupper.io/post/48/defining-class-constants-in-php

Pranav Rana
  • 371
  • 2
  • 6