Php - Constant

Card Puncher Data Processing

About

Language - (Variable) Constant in PHP

Class

See Php - Object

Static Member

Class Constants differ from normal static variables in that you don't use the $ symbol to declare or use them.

class MyClass
{
    const CONSTANT = 'constant value';

    function showConstant() {
        echo  static::CONSTANT . "\n";
    }
}
echo  MyClass::CONSTANT;

Static Method

class MyClass
{

    static function showConstant() {
        echo 'constant value';
    }
}

echo MyClass::showConstant();

Abstract Static Method

Abstract static method

abstract class MyClass
{

   public static abstract function getStaticValue(): string;

   public static function getName(): string
    {
        return static::getStaticValue();
    }
    
}

Reflection

With reflection and the full path of the class (ie classname) and the constant function 1)

$staticValue = constant($className.'::'.$propertyName);

Define

see also: Php - Define (Global Constant)





Discover More
Card Puncher Data Processing
Php - Static Initialization (Variable / Constant)

static in the context of php In static member, you can use the global variable You can't use function in constant initialization. Below are methods that you can use to get the same behavior ...



Share this page:
Follow us:
Task Runner