我在某些类上定义了几个CONST,并希望获得它们的列表。例如:
class Profile {
    const LABEL_FIRST_NAME = "First Name";
    const LABEL_LAST_NAME = "Last Name";
    const LABEL_COMPANY_NAME = "Company";
}有什么方法可以获取在Profile类中定义的CONST的列表吗?据我所知,最接近的option(get_defined_constants())不能解决问题。
我真正需要的是常量名称列表-像这样:
array('LABEL_FIRST_NAME',
    'LABEL_LAST_NAME',
    'LABEL_COMPANY_NAME')要么:
array('Profile::LABEL_FIRST_NAME', 
    'Profile::LABEL_LAST_NAME',
    'Profile::LABEL_COMPANY_NAME')甚至:
array('Profile::LABEL_FIRST_NAME'=>'First Name', 
    'Profile::LABEL_LAST_NAME'=>'Last Name',
    'Profile::LABEL_COMPANY_NAME'=>'Company')
                  您可以使用反射来做到这一点。在该页面上搜索“打印类常量”以查看示例。
                
                
                  
                    —
                    09年
                    
                  
                
              
                  使用Reflection和Cl上的ReflectionClass,可以使用函数getConstants nz.php.net/manual/en/class.reflectionclass.php
                
                
                  
                    —
                    Tim Ebenezer,2009年