Enums in PHP

Who knew enums could be so difficult in PHP 5.3?!? There are probably better options with higher versions of PHP, but I’m not to that point yet.

I tried to use the PHP provided SplEnum, but I could not get it to work with all of the default install of PHP. Maybe I had something wrong, or needed to enabled something in the php.ini file, but either way, enums should not be this hard.

[19-Jul-2015 15:15:33] PHP Fatal error: Class ‘SplEnum’ not found in C:\dev\php\workspace\project\StatusEnum.php on line 2

There are a few good options of enum classes that others have created out on the web, so instead of re-inventing the wheel, I borrowed one of the simpler enum implementations.

abstract class MyEnum
    final public function __construct($value)
        $c = new ReflectionClass($this);
        if(!in_array($value, $c->getConstants())) {
            throw IllegalArgumentException();
        $this->value = $value;

    final public function __toString()
        return $this->value;

class Foo extends MyEnum
    const FOO = "foo";
    const BAR = "bar";

$a = new Foo(Foo::FOO);
$b = new Foo(Foo::BAR);
$c = new Foo(Foo::BAR);

if($a == Foo::FOO) {
    echo "My value is Foo::FOO\n";
} else {
    echo "I dont match!\n";

if($a == $b) {
    echo "a value equals b value!\n";
if($b == $c) {
    echo "b value equals c value!\n";

I think I may extend this to be a little more java-like (hasKey(), etc.), but for now, the most basic enum class above will work great for my needs.

Enable SSH2 in PHP on Windows

Anything that you do with PHP on Windows that is outside of the standard install is always an adventure. Especially installing extensions. This Law of PHP on Windows was again confirmed when I tried to enable SSH2 in PHP 5.3.3 on my Windows machine.

To save you time, here are the steps:

  • Determine which version of PHP you have: x64 or x32, as well as Thread Safe vs. Non-Thread Safe
  • C:\tmp>php -i > info.txt
  • then open info.txt and take note of the “Architecture” as well as the “Thread Safety” (enabled means you need the TS version)
  • Download the SSH2 related files for your version and whether your local install is TS or NTS
  • http://pecl.php.net/package/ssh2/0.12/windows
  • Unzip to your directory of choice
  • Copy libssh2.dll to C:\Windows\System32
  • if on a x64 system, you’ll also need to copy it to C:\Windows\SysWOW64
  • Copy php_ssh2.dll and php_ssh2.pdb to your PHP install directory’s /ext directory (e.g. C:\dev\php\5.3.3\ext)
  • Restart Apache

To test if SSH2 is installed, re-run the “php -i > info.txt” command and ensure that ssh2 related items are listed within the “PHP Streams” section.

Also, you can verify it within your code by running something like:

        if (!function_exists('ssh2_connect')) { 
            echo "ssh2 is not configured properly"