當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP ReflectionClass getConstants()用法及代碼示例

ReflectionClass::getConstants()函數是PHP中的內置函數,用於返回指定常量名稱的數組。

用法:

array ReflectionClass::getConstants( void )

參數:該函數不接受任何參數。


返回值:此函數返回指定常量名稱的數組。

以下示例程序旨在說明PHP中的ReflectionClass::getConstants()函數:

示例1:

<?php 
  
// Declaring a class named as Company 
class Company { 
      
    // Defining some constants 
    const First = "GeeksforGeeks"; 
    const Second = "GFG"; 
} 
  
// Using the ReflectionClass() function  
// over the Company class 
$A = new ReflectionClass('Company'); 
  
// Calling the getConstants() function 
$a = $A->getConstants(); 
  
// Getting an array of the constants 
print_r($a); 
?>

輸出:

Array
(
    [First] => GeeksforGeeks
    [Second] => GFG
)

示例2:

<?php 
  
// Declaring a class named as Departments 
class Departments { 
      
    // Defining some constants 
    const First = "CSE"; 
    const Second = "ECE"; 
    const Third = "EE"; 
    const Fourth = "Mechanical"; 
} 
  
// Using the ReflectionClass() function  
// over the Departments class 
$A = new ReflectionClass('Departments'); 
  
// Calling the getConstants() function 
$a = $A->getConstants(); 
  
// Getting an array of the constants 
print_r($a); 
?>

輸出:

Array
(
    [First] => CSE
    [Second] => ECE
    [Third] => EE
    [Fourth] => Mechanical
)

參考: https://www.php.net/manual/en/reflectionclass.getconstants.php



相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 PHP | ReflectionClass getConstants() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。