php constant arrays/ enums

drizzt81

[H]F Junkie
Joined
Jan 21, 2004
Messages
12,361
Hello

From my understanding, PHP does not offer a `built-in' enum type. People usually recommend using an array to model an enum, since they are a map(key, value). Maybe I am misinterpreting this advice, but I have built a class where the set methods check that the parameter is a value in such a map, before changing the underlying, private property. Example:

Code:
[color=lightblue]class[/color] LoanData{
	[color=lightblue]private[/color] $UndertakingType;
	[color=lightblue]private static[/color] $EnumUndertaking = [color=lightblue]array[/color]('Kauf', 'Bau', 'Renovierung',
								 'Anschlussfinanzierung',
								 'Kapitalbeschaffung');
	[color=lightblue]function[/color] setUndertakingType($type){
		[color=lightblue]self[/color]::checkEnum($this->UndertakingType, [color=lightblue]self[/color]::$EnumUndertaking, $type);
	}
	[color=lightblue]private static function[/color] checkEnum(&$val, &$list, $type){
		if(!in_array($type, $list)){
			die('Typed Valued out of bounds');
		}
		$val = $type;
	}
}

While this works, the problem is that external classes cannot access the arrays directly. Either i'd have to make them public, which defeats the purpose since they should not be modified, or I'd have to add another get function for each of the arrays. What I would like to do, is make them public, but constant, i.e. read-only. From reading the php documentation, the language does not support this. As an alternative, I am considering to create a class for each of these enumerated properties, but I `feel' that would be excessive. I would love to hear your take on this issue.
 
Actually it should be a pretty simple fix. You should be using the 'protected' visibility rather the private. An easy mistake for someone coming from a 'normal' object model ;)

From the manual
The visibility of a property or method can be defined by prefixing the declaration with the keywords: public, protected or private. Public declared items can be accessed everywhere. Protected limits access to inherited and parent classes (and to the class that defines the item). Private limits visibility only to the class that defines the item.
 
Actually it should be a pretty simple fix. You should be using the 'protected' visibility rather the private. An easy mistake for someone coming from a 'normal' object model ;)

From the manual

Do I understand that you suggest I write a class extending LoanData which would handle the typing? Otherwise I am not sure how using protected access instead of private would help me.
 
I think the primary problem is that you have German in there... :p
 
Do I understand that you suggest I write a class extending LoanData which would handle the typing? Otherwise I am not sure how using protected access instead of private would help me.

I guess that was my point, I misread the following line:

While this works, the problem is that external classes cannot access the arrays directly.

To mean your extended classes. My apologies, I answered after reading that thinking it was just a misunderstanding of the quirks in PHP OOP.

It could help to see how you would like to use this in practice. It is easy enough to use an array in PHP similarly to using enums in C++ for example. An array is basically a more feature rich (but sloppy) version of an enum. If you want restricted access to the list then you are going to have to setup a class similarly to how you have done and implement an additional get function (like you mentioned).
 
I guess that was my point, I misread the following line:

To mean your extended classes. My apologies, I answered after reading that thinking it was just a misunderstanding of the quirks in PHP OOP.
no need to apologize, I am happy that you chose to try and help me. It's appreciated. If nothing else, you reminded me that I ought to make some fields (properties) available to extended classes, should they be created later on.
It could help to see how you would like to use this in practice. It is easy enough to use an array in PHP similarly to using enums in C++ for example. An array is basically a more feature rich (but sloppy) version of an enum. If you want restricted access to the list then you are going to have to setup a class similarly to how you have done and implement an additional get function (like you mentioned).

thanks :)
 
Back
Top