Hướng dẫn dùng exists definition trong PHP

[PHP 4, PHP 5, PHP 7, PHP 8]

definedChecks whether a given named constant exists

Description

defined[string $constant_name]: bool

Note:

If you want to see if a variable exists, use isset[] as defined[] only applies to constants. If you want to see if a function exists, use function_exists[].

Parameters

constant_name

The constant name.

Return Values

Returns true if the named constant given by constant_name has been defined, false otherwise.

Examples

Example #1 Checking Constants

See Also

  • define[] - Defines a named constant
  • constant[] - Returns the value of a constant
  • get_defined_constants[] - Returns an associative array with the names of all the constants and their values
  • function_exists[] - Return true if the given function has been defined
  • The section on Constants

daniel at neville dot tk

14 years ago

My preferred way of checking if a constant is set, and if it isn't - setting it [could be used to set defaults in a file, where the user has already had the opportunity to set their own values in another.]



Dan.

ASchmidt at Anamera dot net

5 years ago

// Checking the existence of a class constant, if the class is referenced by a variable.

class Class_A
{
    const CONST_A = 'value A';
}

// When class name is known.
if [ defined[ 'Class_A::CONST_A' ] ]
    echo 'Class_A::CONST_A defined';

// Using a class name variable. Note the double quotes.
$class_name = Class_A::class;
if [ defined[ "$class_name::CONST_A" ] ]
    echo '$class_name::CONST_A defined';

// Using an instantiated object for a variable class.
$object_A = new $class_name[];
if [ defined[ get_class[$object_A].'::CONST_A' ] ]
    echo '$object_A::CONST_A defined';

tris+php at tfconsulting dot com dot au

13 years ago

Before using defined[] have a look at the following benchmarks:

true                                       0.65ms
$true                                      0.69ms [1]
$config['true']                            0.87ms
TRUE_CONST                                 1.28ms [2]
true                                       0.65ms
defined['TRUE_CONST']                      2.06ms [3]
defined['UNDEF_CONST']                    12.34ms [4]
isset[$config['def_key']]                  0.91ms [5]
isset[$config['undef_key']]                0.79ms
isset[$empty_hash[$good_key]]              0.78ms
isset[$small_hash[$good_key]]              0.86ms
isset[$big_hash[$good_key]]                0.89ms
isset[$small_hash[$bad_key]]               0.78ms
isset[$big_hash[$bad_key]]                 0.80ms

PHP Version 5.2.6, Apache 2.0, Windows XP

Each statement was executed 1000 times and while a 12ms overhead on 1000 calls isn't going to have the end users tearing their hair out, it does throw up some interesting results when comparing to if[true]:

1] if[$true] was virtually identical
2] if[TRUE_CONST] was almost twice as slow - I guess that the substitution isn't done at compile time [I had to double check this one!]
3] defined[] is 3 times slower if the constant exists
4] defined[] is 19 TIMES SLOWER if the constant doesn't exist!
5] isset[] is remarkably efficient regardless of what you throw at it [great news for anyone implementing array driven event systems - me!]

May want to avoid if[defined['DEBUG']]...

r dot hartung at roberthartung dot de

12 years ago

You can use the late static command "static::" withing defined as well. This example outputs - as expected - "int [2]"

Lars Lernestal

10 years ago

if you want to check id a class constant is defined use self:: before the constant name:

Shaun H

14 years ago

I saw that PHP doesn't have an enum function so I created my own. It's not necessary, but can come in handy from time to time.

passerbyxp at gmail dot com

9 years ago

This function, along with constant[], is namespace sensitive. And it might help if you imagine them always running under the "root namespace":

Chủ Đề