如果您想要傳回類別內定義的常數,您也可以定義一個內部方法如下
<?php
class myClass {
const NONE = 0;
const REQUEST = 100;
const AUTH = 101;
// 其他...
static function getConstants() {
$oClass = new ReflectionClass(__CLASS__);
return $oClass->getConstants();
}
}
?>
(PHP 5, PHP 7, PHP 8)
ReflectionClass::getConstants — 取得常數
filter
可選的篩選器,用於篩選所需的常數可見性。它是使用ReflectionClassConstant 常數配置的,預設為所有常數可見性。
一個常數的 陣列,其中鍵保存名稱,值保存常數的值。
版本 | 描述 |
---|---|
8.0.0 |
已新增 filter 。 |
如果您想要傳回類別內定義的常數,您也可以定義一個內部方法如下
<?php
class myClass {
const NONE = 0;
const REQUEST = 100;
const AUTH = 101;
// 其他...
static function getConstants() {
$oClass = new ReflectionClass(__CLASS__);
return $oClass->getConstants();
}
}
?>
您可以將 $this 作為 ReflectionClass 的類別傳遞。如果您擴充原始類別,__CLASS__ 將無法協助,因為它是基於檔案本身的魔術常數。
<?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());
// 結果:
array ( size = 2)
'TYPE_A' => int 1
'TYPE_B' => (string) 'hello'
如果您想要定義一個可使用繼承的靜態 getConstants() 函數,您可以執行以下操作
<?php
abstract class AbstractClass
{
const TEST = "test";
public static function getConstants()
{
// 這裡的 "static::class" 發揮了魔法
$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());
// 結果:
array(3) {
'TYPE_A' => int(1)
'TYPE_B' => string(5) "hello"
'TEST' => string(4) "test"
}
?>
我使用函式根據類別常數名稱執行某些操作,如下所示。這個範例可能對大家有幫助。
<?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)){
//.....執行某些操作
}
}
?>
我試著確定如何取得介面中常數的 var_dump。沒錯,不是使用任何類別,而是使用介面本身。
在我的旅程中,我發現 ReflectionClass 與直接呼叫介面也會傾印其常數,這相當有趣。完美!!!!
這是使用 PHP 5.3.1,而我的範例如下:-
第 1 個檔案
constants.php
<?php
<?php>
interface MyConstants
{
// --------------------------
// 程式級別
// --------------------------
const DEBUG_MODE_ACTIVE = FALSE;
const PHP_VERSION_REQUIREMENT = "5.1.2";
}
?>
=======
第二個檔案
=======
test.php
<?php>
include_once ("constants.php");
$oClass = new ReflectionClass ('MyConstants');
$array = $oClass->getConstants ();
var_dump ($array);
unset ($oClass);
?>
您會從命令列取得的內容:-
?:\???\htdocs\????>php test.php
array(2) {
["DEBUG_MODE_ACTIVE"]=> bool(false)
["PHP_VERSION_REQUIREMENT"]=> string(5) "5.1.2"
但正如你所見,這在許多方面對你都很有利,所以我真心希望這能幫助未來有類似困擾的人!
請享用!
取得宣告的最新常數。
abstract class AbstractEnum
{
/**
* Возвращает все константы класса || 傳回所有常數
*
* @return array
*/
static function getConstants()
{
$rc = new \ReflectionClass(get_called_class());
return $rc->getConstants();
}
/**
* Возвращает массив констант определенные в вызываемом классе || 傳回在呼叫類別中定義的最後常數
*
* @return array
*/
static function lastConstants()
{
$parentConstants = static::getParentConstants();
$allConstants = static::getConstants();
return array_diff($allConstants, $parentConstants);
}
/**
* Возвращает все константы родительских классов || 傳回父類別的所有常數
*
* @return array
*/
static function getParentConstants()
{
$rc = new \ReflectionClass(get_parent_class(static::class));
$consts = $rc->getConstants();
return $consts;
}
}
======
class Roles extends AbstractEnum
{
const ROOT = 'root';
const ADMIN = 'admin';
const USER = 'user';
}
// 輸出
全部:root, admin, user
最後:root, admin, user
class NewRoles extends Roles
{
const CLIENT = 'client';
const MODERATOR = 'moderator';
const SUPERMODERATOR = 'super'.self::USER;
}
// 輸出
全部:client, moderator, superuser, root, admin, user
最後:client, moderator, superuser
class AdditionalRoles extends Roles
{
const VIEWER = 'viewer';
const CHECKER = 'checker';
const ROOT = 'rooter';
}
全部:viewer, checker, rooter, client, moderator, superuser, admin, user
最後:viewer, checker, rooter