略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: get_defined_constants

2024-12-22

get_defined_constants

(PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8)

get_defined_constants返回所有常量的关联数组,键是常量名,值是常量值

说明

get_defined_constants(bool $categorize = false): array

返回当前所有已定义的常量名和值。 这包含 define() 函数所创建的,也包含了所有扩展所创建的。

参数

categorize

让此函数返回一个多维数组,分类为第一维的键名,常量和它们的值位于第二维。

<?php
define
("MY_CONSTANT"1);
print_r(get_defined_constants(true));
?>

以上例程的输出类似于:

Array
(
    [Core] => Array
        (
            [E_ERROR] => 1
            [E_WARNING] => 2
            [E_PARSE] => 4
            [E_NOTICE] => 8
            [E_CORE_ERROR] => 16
            [E_CORE_WARNING] => 32
            [E_COMPILE_ERROR] => 64
            [E_COMPILE_WARNING] => 128
            [E_USER_ERROR] => 256
            [E_USER_WARNING] => 512
            [E_USER_NOTICE] => 1024
            [E_ALL] => 2047
            [TRUE] => 1
        )

    [pcre] => Array
        (
            [PREG_PATTERN_ORDER] => 1
            [PREG_SET_ORDER] => 2
            [PREG_OFFSET_CAPTURE] => 256
            [PREG_SPLIT_NO_EMPTY] => 1
            [PREG_SPLIT_DELIM_CAPTURE] => 2
            [PREG_SPLIT_OFFSET_CAPTURE] => 4
            [PREG_GREP_INVERT] => 1
        )

    [user] => Array
        (
            [MY_CONSTANT] => 1
        )

)

返回值

返回的数组为 常量名 => 常量值,也可以按注册变量的扩展名称来分组。

更新日志

版本 说明
5.3.1 仅作用于 Windows:内核常量归类到 Core,之前是 mhash
5.3.0 内核常量归类为 Core,之前是 internal。在 Windows 上,内核常量归类到 mhash
5.2.11 categorize 参数现在可以合适得被处理。 在此之前,categorize 被解释为 !is_null($categorize),导致任何非 null 的值会强制常量分类。

范例

示例 #1 get_defined_constants() 例子

<?php
print_r
(get_defined_constants());
?>

以上例程的输出类似于:

Array
(
    [E_ERROR] => 1
    [E_WARNING] => 2
    [E_PARSE] => 4
    [E_NOTICE] => 8
    [E_CORE_ERROR] => 16
    [E_CORE_WARNING] => 32
    [E_COMPILE_ERROR] => 64
    [E_COMPILE_WARNING] => 128
    [E_USER_ERROR] => 256
    [E_USER_WARNING] => 512
    [E_USER_NOTICE] => 1024
    [E_ALL] => 2047
    [TRUE] => 1
)

参见

add a noteadd a note

User Contributed Notes 5 notes

up
21
Bob
13 years ago
Add this method to your class definition if you want an array of class constants (get_defined_constants doesn't work with class constants as Peter P said above):

<?php
public function get_class_constants()
{
   
$reflect = new ReflectionClass(get_class($this));
    return
$reflect->getConstants());
}
?>

You could also override stdObject with it so that all your classes  have this method
up
8
R4FC0R3
4 years ago
If you want to access directly on one category, just use:

<?php

print_r
(get_defined_constants(true)['Core']);

?>

you can replace 'Core' by the category you wish (e.g. user):

<?php

print_r
(get_defined_constants(true)['user']);

?>

Warning: only use this on development side
up
6
Anonymous
16 years ago
If you want to filter through and return only the prefix for your constants (i.e. you have constants with a naming scheme), then you can use this quick little function. It comes in handy for debugging.

<?php
function returnConstants ($prefix) {
    foreach (
get_defined_constants() as $key=>$value)
        if (
substr($key,0,strlen($prefix))==$prefix$dump[$key] = $value;
    if(empty(
$dump)) { return "Error: No Constants found with prefix '".$prefix."'"; }
    else { return
$dump; }
}
?>

Example:

<?php
define
("SITENAME_OPTION_ONE",true);
define("SITENAME_OPTION_TWO",false);
define("SITENAME_URL","foo");

print_r(returnConstants("SITENAME_OPTION"));
?>

Will return:

Array
(
    [SITENAME_OPTIONONE] => 1
    [SITENAME_OPTIONTWO] =>
)
up
3
me at gogogadgetscott dot info
17 years ago
<?php
/**
* Convert constant value into string name.
*
* @param  mixed  Constant value.
* @return string Constant name.
* @access public
*/
function sch_get_consant($value)
{
   
$constants = get_defined_constants();
   
$name = array_search($value, $constants, TRUE);   
    return
$name;
}
?>
up
0
S-Tian
5 months ago
If you want to create a static method that you can copy and paste into each class you have constants created, you can use:

<php
public static function get_class_constants(): array
{
    $reflect = new ReflectionClass(self::class);

    return $reflect->getConstants();
}
?>

官方地址:https://www.php.net/manual/en/function.get-defined-constants.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3