Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

[Enum Implementation] can I loop through class constants?

Status
Not open for further replies.

GuardianOfTheFlame

Programmer
Sep 4, 2007
31
IT
hi all,
my question is (regarding the ReportType class):
is there a way to loop through the costants defined in the class?

this is my enum implementation:
Code:
final class ReportType {
    const Total = 'total';
    const SpecificCenter = 'specific_center';
    const AllCenters = 'all_centers';
    const Average = 'average';
    // ensures that this class acts like an enum
    // and that it cannot be instantiated
    private function __construct(){}
	public static function isValid($value) {
		switch ($value) {
			case ReportType::Total:
			case ReportType::SpecificCenter:
			case ReportType::AllCenters:
			case ReportType::Average:
				return true;
				break;
			default:
				return false;
				break;
		}
		return true;
	}
}
I use it in function calls:
Code:
$rec = $rep->getRecruitmentData('myStudy', 1, ReportType::AllCenters, 9);
and to validate the function parameter:
Code:
public function getRecruitmentData($project_name, $user_id, $report_type, $center_id) {
		if (!ReportType::isValid($report_type)) {
			throw new InvalidParameterException('Cannot create recruitment report: invalid report type argument.');
			return false;
		}
[...]
}
I would like to create an array of defined costants similar to the result of get_class_vars... I need a sort of "get_class_costants":
Code:
public static function isValid($value) {
    $valid_values = get_class_costants('ReportType');
    return in_array($value, $valid_values);
}

Thanks,
Matteo

---

The surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us - Calvin (and Hobbes) ;-)
 
sure. just use a reflector.

something like this

Code:
class test{
	const CONSTANT = 'foo';
	
}
$t = new test;
$r = new ReflectionObject($t);
print_r($r->getConstants());
 
thank you, it works great! ;)

this is the final version of my enum implementation:
Code:
//Report Type enum implementation
final class ReportType {
    const Total = 'total';
    const SpecificCenter = 'specific_center';
	const AllCenters = 'all_centers';
	const Average = 'average';
    // ensures that this class acts like an enum
    // and that it cannot be instantiated
    private function __construct(){}
	public static function isValid($value) {
		//retrieve the class costants
		$t=new ReportType();
		$r = new ReflectionObject($t);
		$valid_values = $r->getConstants();
		//check if the value is correct
		return in_array ($value, $valid_values);
	}
}

---

The surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us - Calvin (and Hobbes) ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top