PHP 5.3 now accepts the object as the class reference: $this::BOOL_CONST
is now accepted.
//
// http://php.net/manual/en/language.oop5.constants.php
//
// 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).
//
// I renamed "Parent" class name to "constantes"
// because the classname "Parent" can be confused with "parent::" scope
class constantes
{
const test = false;
}
// I renamed "SomeChild" too, with no reason...
class OverloadConst extends constantes
{
const test = true;
public function waysToGetTheConstant()
{
var_dump(array('$this'=>$this::test)); // true, also usable outside the class
var_dump(array('self::'=>self::test)); // true, only usable inside the class
var_dump(array('parent::'=>parent::test)); // false, only usable inside the class
var_dump(array('static::'=>static::test)); // true, should be in class's static methods, see http://php.net/manual/en/language.oop5.late-static-bindings.php
}
}
// Classic way: use the class name
var_dump(array('Using classname' => OverloadConst::test));
// PHP 5.3 way: use the object
$object = new OverloadConst();
var_dump(array('Using object' => $object::test));
$object->waysToGetTheConstant();
Note that you can override a class constant, but not an interface constant.
If constantes
is an interface that OverloadConsts
implements, then you can not override its const test
(or BOOL_CONST
).
Sources