<?php
class MyClass
{
const CONSTANT = 'constant value';
function showConstant() {
echo self::CONSTANT . "
";
}
}
echo MyClass::CONSTANT . "
";
$classname = "MyClass";
echo $classname::CONSTANT . "
";
$class = new MyClass();
$class->showConstant();
echo $class::CONSTANT."
";
?>
//it's possible to declare constant in base class, and override it in child, and access to correct value of the const from the static method is possible by 'get_called_class' method:
<?php
abstract class dbObject
{
const TABLE_NAME='undefined';
public static function GetAll()
{
$c = get_called_class();
return "SELECT * FROM `".$c::TABLE_NAME."`";
}
}
class dbPerson extends dbObject
{
const TABLE_NAME='persons';
}
class dbAdmin extends dbPerson
{
const TABLE_NAME='admins';
}
echo dbPerson::GetAll()."<br>";//output: "SELECT * FROM `persons`"
echo dbAdmin::GetAll()."<br>";//output: "SELECT * FROM `admins`"
?>