Location: PHPKode > scripts > PHP Enumeration > php-enumeration/Enumeration.class.php
<?php
/*------------------------------------------------------------------------------
 * File:		base.php
 * Description:	PHP Enumeration Class
 * Author:		Jim Sierra/Sierra Web & Technology Solutions
 * Homepage:	www.sierrawebtech.com 
 * License : 		GNU/General Public License
 * 			License can be viewed at http://www.opensource.org/licenses/gpl-license.php
 * COPYRIGHT (c) 2008 Jim Sierra
 *
 */

Class enumError extends Exception {};

Class baseEnum 
{
	private $enumArray;
	private $value = '';
	
	public function __set($key,$value)
	{
		// Is this a valid field
		if ($key != 'value')
			throw new enumError('enum only contains value property');

		// Is value a proper enumeration value
		if (!in_array($value, $this->enumArray))
			throw new enumError('invalid value for property');
		
		$this->value = $value;

	}

	public function __get($key)
	{
		// Is this a valid field
		if ($key != 'value')
			throw new enumError('enum only contains value property');

		$a = $this->value;
		return $this->value;

	}

	public function __construct($classname)
	{
		// Get enumeration keys and values from child class constant values
		$x=new ReflectionClass($classname);
		foreach($x->getConstants() as $key=>$value)
	    	$this->enumArray[$key] = $value;
	}
}

?>
Return current item: PHP Enumeration