Ich habe mehrere CONSTs in einigen Klassen definiert und möchte eine Liste von ihnen erhalten. Zum Beispiel:Kann ich CONSTs in einer PHP-Klasse definiert bekommen?
class Profile {
const LABEL_FIRST_NAME = "First Name";
const LABEL_LAST_NAME = "Last Name";
const LABEL_COMPANY_NAME = "Company";
}
Gibt es eine Möglichkeit, eine Liste des CONST des auf der Profile
Klasse definiert zu bekommen? Soweit ich das beurteilen kann, wird die nächste Option (get_defined_constants()
) nicht ausreichen.
Was ich wirklich brauchen, ist eine Liste der Namen von Konstanten - etwa so:
array('LABEL_FIRST_NAME',
'LABEL_LAST_NAME',
'LABEL_COMPANY_NAME')
Oder:
array('Profile::LABEL_FIRST_NAME',
'Profile::LABEL_LAST_NAME',
'Profile::LABEL_COMPANY_NAME')
Oder auch:
array('Profile::LABEL_FIRST_NAME'=>'First Name',
'Profile::LABEL_LAST_NAME'=>'Last Name',
'Profile::LABEL_COMPANY_NAME'=>'Company')
Mit Reflexion und ein Reflection auf Cl, können Sie die Funktion getConstants verwenden http://nz.php.net/manual /en/class.reflectionclass.php –
[Reflection wird dein Retter] (http://se2.php.net/manual/en/reflectionclass.getconstants.php). –
Sie können dies mit [reflection] (http://nz.php.net/oop5.reflection) tun. Suchen Sie nach "Klassenkonstanten drucken" auf dieser Seite, um ein Beispiel zu sehen. – n3rd