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


PHP Reflection getModifiers()用法及代碼示例


Reflection::getModifiers()函數是PHP中的內置函數,用於返回指定修飾符名稱的數組。

用法:

int Reflection::getModifiers( void )

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


返回值:此函數返回指定類的訪問修飾符的位域。

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

示例1:

<?php 
  
// Declaring a class Testing 
class Testing {  
      
    // Calling a function GeeksforGeeks() with 
    // two modifier named as public and static 
    public static function GeeksforGeeks() 
    { 
        return; 
    } 
} 
  
// ReflectionMethod is called on the class Testing and 
// their member as function GeeksforGeeks() 
$GeeksforGeeks = new ReflectionMethod('Testing', 'GeeksforGeeks'); 
  
// Calling the getModifiers() function and printing 
// an array of modifiers 
echo implode(' ', Reflection::getModifierNames( 
                $GeeksforGeeks->getModifiers())); 
?>
輸出:
public static

示例2:

<?php 
   
// Declaring a class Departments 
class Departments {  
       
    // Calling some function with 
    // different modifiers 
    public function IT() { 
        return; 
    } 
    final public function CSE() { 
        return; 
    } 
    private function ECE() { 
        return; 
    } 
} 
   
// ReflectionMethod is called on the above class 
// with their members 
$A = new ReflectionMethod('Departments', 'IT'); 
$B = new ReflectionMethod('Departments', 'CSE'); 
$C = new ReflectionMethod('Departments', 'ECE'); 
   
// Calling the getModifiers() function and printing 
// an array of modifiers 
echo implode(' ', Reflection::getModifierNames( 
                  $A->getModifiers())). "\n"; 
echo implode(' ', Reflection::getModifierNames( 
                  $B->getModifiers())). "\n"; 
echo implode(' ', Reflection::getModifierNames( 
                  $C->getModifiers())). "\n"; 
?>
輸出:
public
final public
private

參考: https://secure.php.net/manual/en/reflectionclass.getmodifiers.php



相關用法


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