当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。