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?
to access the constant try
class A
{
const HELLO = 'HELLO WORLD';
public function __construct()
{
echo self::HELLO;
}
}
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.
It may take up to a week until the changes appear on all mirrors.
Thanks for pointing it out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With