Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing constants inside PHP object [closed]

Currently I'm reading PHP 5 OOP (properties section) and there I found the following statement:

Within class methods the properties, constants, and methods may be accessed by using the form $this->property

That's strange, but I can't access constants using that format. Following code will raise notice:

class A
{
    const HELLO = 'HELLO WORLD';

    public function __construct()
    {
        echo $this->HELLO;
    }
}

$a = new A();

Did I misunderstand something or authors of documentation made a mistake?

like image 359
Wild One Avatar asked Oct 11 '25 14:10

Wild One


2 Answers

to access the constant try

class A
{
    const HELLO = 'HELLO WORLD';

    public function __construct()
    {
        echo self::HELLO;
    }
}
like image 135
bukart Avatar answered Oct 14 '25 03:10

bukart


The entry in the manual was a bit misleading indeed. I have removed the references to constants and methods now, because they don't belong into a chapter about properties anyway. The new paragraph will sound something like this now:

Within class methods non-static properties may be accessed by using -> (Object Operator): $this->property (where property is the name of the property). Static properties are accessed by using the :: (Double Colon): self::$property. See Static Keyword for more information on the difference between static and non-static properties.

  • http://svn.php.net/viewvc?view=revision&revision=328166 and
  • http://svn.php.net/viewvc?view=revision&revision=328167

It may take up to a week until the changes appear on all mirrors.

Thanks for pointing it out.

like image 43
Gordon Avatar answered Oct 14 '25 03:10

Gordon