tag:blogger.com,1999:blog-9264830.post-1155968296732857402006-08-19T08:18:00.000+02:002006-08-19T08:18:16.743+02:00Enumerations in PHPSometimes you stumble over stuff on the net and really think to yourself. Wow! Why does not everybody talk about it yet? My Subject of the day is <a href="http://pecl.php.net/package/SPL_Types">Enumerations in PHP via SPL_Types</a>. Basically it brings Enumerations to PHP, ensuring that a variable may only contain specific values based on a value domain. This should be <a href="http://beeblex.com/lists/index.php/php.pecl.dev/3570">looking something like this</a>.<br /><br /><br /><pre class="block">class Weekday extends SplEnum<br />{<br /> const Sunnday = 0;<br /> const Monday = 1;<br /> const Tuesday = 2;<br /> const Wednesday = 3;<br /> const Thursday = 4;<br /> const Friday = 5;<br /> const Saturday = 6;<br /> const __default = Weekday::Sunday;<br />}<br /><br />$e = new Weekday;<br /><br />var_dump($e); // shows object of type SplEnum<br />var_dump((int)$e); // int(0)<br /><br />$e++;<br /><br />var_dump($e); // shows object of type SplEnum<br />var_dump((int)$e); // int(1)<br />var_dump($e + 3); // int(4)</pre>I have found this example over at a <a href="http://beeblex.com/lists/index.php/php.pecl.dev/3570">mailing list transcript at BeebleX</a>. While there is certainly a trace of this functionality in the <a href="http://cvs.php.net/viewvc.cgi/pecl/spl_types/">CVS Repository</a> at php.net you cannot yet get a precompiled version of the extension at <a href="http://snaps.php.net">PHP-Snaps</a> :-(.<br /><br />Personal note: While I love PHP for the fact that it's a dynamically typed language, I consider Enumerations a big help, to communicate and ensure consistency of parameter values on public interfaces.WittRaider's Bloghttp://www.blogger.com/profile/05216896734950208670noreply@blogger.com