Martin Majer
Martin Majer

Reputation: 3352

Check if a class constant exists

How can I check if a constant is defined in a PHP class?

class Foo {
    const BAR = 1;
}

Is there something like property_exists() or method_exists() for class constants? Or can I just use defined("Foo::BAR")?

Upvotes: 75

Views: 51453

Answers (6)

Josep Alsina
Josep Alsina

Reputation: 3027

If you are working with inheritance and you want to check if a constant is defined at the point where you want to access it at runtime, you can use late static binding (since PHP 5.3).

$reflectionClass = new \ReflectionClass(static::class);

if (!array_key_exists('MY_CONST', $reflectionClass->getConstants())){
    throw new \LogicException('You must define the constant MY_CONST in the concrete class');
}

This can be used as a workaround to make sure a concrete class defines a constant needed elsewhere along the class hierarchy. Obviously, we need to keep in mind the caveat that this is done at run time. But if you add this check in the tests in your CI pipeline it's a good enough workaround.

Upvotes: -1

pamekar
pamekar

Reputation: 739

So I tried this:

$constants = new \ReflectionClass(App\Namespace\ClassName::class);
if ($constants->getConstant('CONSTANT_NAME')){
    // Do this
} else {
    // Do that
}

And it worked fine.

Upvotes: 1

Kamafeather
Kamafeather

Reputation: 9835

You have 3 ways to do it:

defined()

[PHP >= 4 - most retro-compatible way]

$class_name = get_class($object); // remember to provide a fully-qualified class name
$constant = "$class_name::CONSTANT_NAME";
$constant_value = defined($constant) ? $constant : null;

Note: using defined() on a private constant (possible from PHP7.1) will throw error: "Cannot access private const". While using ReflectionClass or ReflectionClassConstant will work.

ReflectionClass

[PHP >= 5]

$class_reflex = new \ReflectionClass($object);
$class_constants = $class_reflex->getConstants();
if (array_key_exists('CONSTANT_NAME', $class_constants)) {
    $constant_value = $class_constants['CONSTANT_NAME'];
} else {
    $constant_value = null;
}

ReflectionClassConstant

[PHP >= 7.1.0]

$class_name = get_class($object); // fully-qualified class name
try {
    $constant_reflex = new \ReflectionClassConstant($class_name, 'CONSTANT_NAME');
    $constant_value = $constant_reflex->getValue();
} catch (\ReflectionException $e) {
    $constant_value = null;
}

There is no real better way. Depends on your needs and use case.

Upvotes: 21

Savageman
Savageman

Reputation: 9477

Yes, just use the class name in front of the constant name:

defined('SomeNamespace\SomeClass::CHECKED_CONSTANT');

http://www.php.net/manual/en/function.defined.php#106287

Upvotes: 72

quant2016
quant2016

Reputation: 477

You can use that function:

function constant_exists($class, $name){
    if(is_string($class)){
        return defined("$class::$name");
    } else if(is_object($class)){
        return defined(get_class($class)."::$name");
    }
    return false;
}

Or alternative version using ReflectionClass

function constant_exists($class, $name) {
    if(is_object($class) || is_string($class)){
        $reflect = new ReflectionClass($class);
        return array_key_exists($name, $reflect->getConstants());
    }
    return false;
}

Upvotes: 11

Daan
Daan

Reputation: 12236

You can check if a constant is defined with the code below:

<?php
if(defined('className::CONSTANT_NAME')){
  //defined
}else{
  //not defined
}

Upvotes: 89

Related Questions