Check If a Constant Is Already Defined

How to check if a defined constant exists in PHP?

First, these are not variables, but constants.

And you can check their existence by using the defined() function :

bool defined ( string $name )

Checks whether the given constant exists and is defined.

Check if a constant is already defined


CONST = 2 unless defined? CONST

See here for more about awesome defined? operator.

P.S. And in the future I guess you'll want var ||= 1 instead of var = var||1.

Right way to check if a constant is already defined in a Ruby Class

The keyword defined? is documented here.

It is better to ask if it is a constant, and use const_defined? if it is important that it is a constant. If you only care that it is defined, then use the keyword defined?

constant already defined in php

Replace this:

define('constant', 'value');

with this:

if (!defined('constant')) define('constant', 'value');

How do I check if a constant is defined from a different class?

Probably works better with a class name instead of an object variable :

if(defined(get_class($foo).'::BAR')) // Do something

Check if a class constant exists

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

How to check if a constant is defined by its symbol in Ruby?

Use const_defined? instead: http://ruby-doc.org/core/classes/Module.html#M000487

Object.const_defined?(const.to_s)


Related Topics



Leave a reply



Submit