ReflectionClass::getConstants

(PHP 5, PHP 7)

ReflectionClass::getConstants定数を取得する

説明

public ReflectionClass::getConstants ( void ) : array

可視性にかかわらず、クラスで定義されているすべての定数を取得します。

パラメータ

この関数にはパラメータはありません。

返り値

定数の配列を返します。 キーは名前を保持し、値は定数の値を保持します。

参考

add a note add a note

User Contributed Notes 5 notes

up
24
davide dot renzi at gmail dot com
4 years ago
If you want to return the constants defined inside a class then you can also define an internal method as follows:

<?php
class myClass {
    const
NONE = 0;
    const
REQUEST = 100;
    const
AUTH = 101;

   
// others...

   
static function getConstants() {
       
$oClass = new ReflectionClass(__CLASS__);
        return
$oClass->getConstants();
    }
}
?>
up
7
Sandor Toth
2 years ago
You can pass $this as class for the ReflectionClass. __CLASS__ won't help if you extend the original class, because it is a magic constant based on the file itself.

<?php

class Example {
  const
TYPE_A = 1;
  const
TYPE_B = 'hello';

  public function
getConstants()
  {
   
$reflectionClass = new ReflectionClass($this);
    return
$reflectionClass->getConstants();
  }
}

$example = new Example();
var_dump($example->getConstants());

// Result:
array ( size = 2)
 
'TYPE_A' => int 1
 
'TYPE_B' => (string) 'hello'
up
1
shgninc at gmail dot com
5 years ago
I use a functions to do somthing base on the class constant name as below. This example maybe helpful for everybody.
<?php
public function renderData($question_type = NULL, $data = array()) {
       
$types = array();
       
$qt = new ReflectionClass(questionType);
       
$types = $qt->getConstants();
        if (
$type = array_search($question_type, $types)){
               
//.....Do somthing
}
}
?>
up
3
djhob1972 at yahoo dot com dot au
9 years ago
I was trying to determine how to get a var_dump of constants that are within an interface.  Thats right, not using any classes but the interface itself.

Along my travels I found it quite interesting that the ReflectionClass along with a direct call to the interface will also dump its constants.  Perfect!!!!

This was using PHP 5.3.1 and my example as below:-

1st File:

constants.php

<?php
<?php>

interface
MyConstants
{
   
// --------------------------
    // Programmatic Level
    // --------------------------
   
const DEBUG_MODE_ACTIVE       = FALSE;
    const
PHP_VERSION_REQUIREMENT = "5.1.2";
}
?>

=======
Second file:
=======

test.php

<?php>
include_once ("constants.php");

$oClass = new ReflectionClass ('MyConstants');
$array = $oClass->getConstants ();
var_dump ($array);
unset ($oClass);
?>

what you would get from the command line:-

?:\???\htdocs\????>php test.php
array(2) {
  ["DEBUG_MODE_ACTIVE"]=> bool(false)
  ["PHP_VERSION_REQUIREMENT"]=> string(5) "5.1.2"

But as you can see this can work quite well to your advantage in many ways so I truely hope this helps someone else with a similar headache in the future to come!

Enjoy!
up
1
Panni
4 months ago
If you want to define a static getConstants() function which works with inheritance you can do the following:

<?php

abstract class AbstractClass
{
    const
TEST = "test";

    public static function
getConstants()
    {
       
// "static::class" here does the magic
       
$reflectionClass = new ReflectionClass(static::class);
        return
$reflectionClass->getConstants();
    }
}

class
ChildClass extends AbstractClass
{
    const
TYPE_A = 1;
    const
TYPE_B = 'hello';
}

$example = new ChildClass();
var_dump($example->getConstants());

// Result:
array(3) {
 
'TYPE_A' => int(1)
 
'TYPE_B' => string(5) "hello"
  'TEST'
=> string(4) "test"
}

?>
To Top