How to Examine Defined Constants in PHP

How do I examine defined constants in PHP?

Take a look at the get_defined_constants function. It will return an array of all the defined constants in the code up to the point of the function call. You can then use print_r to print out the array.

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.

get defined constants from current file only

This can be done through get_defined_constant() method. Following will be the code to get those values.

include("/path_to_file/constant_file.php");

$constant_set = get_defined_constants(true);

echo $constant_set['user']['staff'];
echo $constant_set['user']['manager'];
echo $constant_set['user']['director'];

The working script with url: http://sugunan.net/demo/const.php

How to check if the value of a PHP defined constant equals something?

define('WPLANG', 'some value');
if(WPLANG == 'some value'){
...
...
}

Or

define('WPLANG', 1212);
if(WPLANG == 1212){
...
...
}

It's good to check if the constant is defined before checking its value, since if ( I_AM_UNDEFINED ) will always be false, because the value of an undefined constant is null. If you assume it exists and it doesn't, then you could get some unexpected results. So:

if( defined('WPLANG') && WPLANG === 'some value' ) {

Correct way to check if a class has a constant defined with PHPUnit

I'm using Reflection class for this purpose. It has getConstants method which returns an associative array [<constant_name> => <constant_value>, ...].

Something like:

public function testHasSiteExportedConstant()
{
$mailer = new \ReflectionClass(SiteExporter::class);
$this->assertArrayHasKey('SITE_EXPORTED', $mailer->getConstants());
}

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

Proper usage of constants in PHP

If you find yourself setting a variable for convenience and never changing it during a script, the chances are you should be using a constant instead. Constants are like variables except that once they are defined they cannot be undefined or changed - they are constant as the name suggests. In many languages, constants are faster than variables and so are recommended, but this is not the case as much in PHP - although they are perhaps a small amount faster, the primary advantage to using constants is the fact that they do not have a dollar sign at the front, and so are visibly different from variables. Furthermore, constants are automatically global across your entire script, unlike variables.

To set a constant, use the define() function - it takes two parameters, with the first being the name of the constant to set, and the second being the value to which you wish to set it. For example, this following line of code sets the variable CURRENT_TIME to be the return value of the time() function, then prints it out:

define("CURRENT_TIME", time());
print CURRENT_TIME;

Note that it is not $CURRENT_TIME or Current_Time - constants, like variables, are case sensitive, but unlike variables they do not start with a dollar sign. You can change this behaviour by passing true as a third parameter to define(), which makes the constant case-insensitive:

define("CURRENT_TIME", time(), true);
print Current_TiMe;

There are two helpful functions available for working with constants, and these are defined() and constant(). The defined() function is basically the constant equivalent of isset(), as it returns true if the constant string you pass to it has been defined. For example:

define("CURRENT_TIME", time(), true);
if (defined("CURRENT_time")) {
/// etc }

Note that you should pass the constant name into defined() inside quotes.

How to check whether a class defines a const (feature detection)

You may use ReflectionClass::getClassConstants() which will output all defined constants or ReflectionClass::getClassConstant() which will output the result for the given constant. Please see the example below.

If you can extend the class, add the static function like:

class myClass
{
const TEST = 'foo';
static function getConstants()
{
$oClass = new \ReflectionClass(__CLASS__);
return $oClass->getConstants();
}
}

output all constants:

var_dump(myClass::getConstants());

If you can't extend the class, you can check with this in any file:

$c = new myClass();
$a = new \ReflectionClass($c);
var_dump( $a->getConstant('FOO'));
// false
var_dump( $a->getConstant('TEST'));
// string(3) "foo"

// Or with a real check:

if ($a->getConstant('TEST')) {
// constant is defined
} else {
// it is not
}


Related Topics



Leave a reply



Submit