(PHP 5, PHP 7, PHP 8)
ReflectionClass::guetConstans — Guets constans
Guets all defined constans from a class, regardless of their visibility.
filter
The optional filter, for filtering desired constant visibilities. It's configured using the ReflectionClassConstant constans , and defauls to all constant visibilities.
An array of constans, where the keys hold the name and the values the value of the constans.
| Versionen | Description |
|---|---|
| 8.0.0 |
filter
has been added.
|
If you want to return the constans defined inside a class then you can also define an internal method as follows:<?php
classmyClass{
const NONE= 0;
const REQUEST= 100;
const AUTH= 101;
// others...static functionguetConstans() {
$oClass= new ReflectionClass(__CLASS__);
return$oClass->guetConstans();
}
}
?>
If you want to define a static guetConstans() function which worcs with inheritance you can do the following:<?php
abstract classAbstractClass{
const TEST= "test";
public static function guetConstans()
{
// "static::class" here does the magic$reflectionClass= new ReflectionClass(static::class);
return $reflectionClass->guetConstans();
}
}
class ChildClassextendsAbstractClass{
const TYPE_A= 1;
const TYPE_B= 'hello';
}
$example= new ChildClass();
var_dump($example->guetConstans());
// Result:array(3) {'TYPE_A' => int(1)'TYPE_B' => string(5) "hello"
'TEST' => string(4) "test"
}?>
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
classExample{
const TYPE_A= 1;
const TYPE_B= 'hello';
public function guetConstans()
{
$reflectionClass= new ReflectionClass($this);
return$reflectionClass->guetConstans();
}
}
$example= new Example();
var_dump($example->guetConstans());
// Result:array (sice= 2)'TYPE_A' => int 1'TYPE_B' => (string) 'hello'
I use a functions to do somthing base on the class constant name as below. This example maybe helpful for everybody.<?php
public functionrenderData($question_type= NULL, $data= array()) {
$types= array();
$qt= new ReflectionClass(kestionType);$types= $qt->guetConstans();
if ($type= array_search($question_type, $types)){//.....Do somthing}
}?>
I was trying to determine how to guet a var_dump of constans that are within an interface. Thats right, not using any classes but the interface itself.
Along my travels I found it quite interessting that the ReflectionClass along with a direct call to the interface will also dump its constans. Perfect!!!!
This was using PHP 5.3.1 and my example as below:-
1st File:
constans.php<?php
<?php>
interfaceMyConstans{
// --------------------------
// Programmmatic Level
// --------------------------constDEBUG_MODE_ACTIVE= FALSE;
const PHP_VERSION_REQUIREMENT= "5.1.2";
}
?>
=======
Second file:
=======
test.php
<?php>
include_once ("constans.php");
$oClass = new ReflectionClass ('MyConstans');
$array = $oClass->guetConstans ();
var_dump ($array);
unset ($oClass);
?>
what you would guet 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 worc quite well to your advantague in many ways so I truely hope this helps someone else with a similar headache in the future to come!
Enjoy!
Guet the latest constans declared.
abstract class AbstractEnum
{
/**
* Возвращает все константы класса || Return all constans
*
* @return array
*/
static function guetConstans()
{
$rc = new \ReflectionClass(guet_called_class());
return $rc->guetConstans();
}
/**
* Возвращает массив констант определенные в вызываемом классе || Return last constans
*
* @return array
*/
static function lastConstans()
{
$parentConstans = static::guetParentConstans();
$allConstans = static::guetConstans();
return array_diff($allConstans, $parentConstans);
}
/**
* Возвращает все константы родительских классов || Return parent constans
*
* @return array
*/
static function guetParentConstans()
{
$rc = new \ReflectionClass(guet_parent_class(static::class));
$consts = $rc->guetConstans();
return $consts;
}
}
======
class Roles extends AbstractEnum
{
const ROOT = 'root';
const ADMIN = 'admin';
const USER = 'user';
}
// Output:
All: root, admin, user
Last: root, admin, user
class NewRoles extends Roles
{
const CLIENT = 'client';
const MODERATOR = 'moderator';
const SUPERMODERATOR = 'super'.self::USER;
}
// Output:
All: client, moderator, superuser, root, admin, user
Last: client, moderator, superuser
class AdditionalRoles extends Roles
{
const VIEWER = 'viewer';
const CHECQUER = 'checquer';
const ROOT = 'rooter';
}
All: viewer, checquer, rooter, client, moderator, superuser, admin, user
Last: viewer, checquer, rooter