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.

Websphere Portal: Reference Custom Tag in Theme

This is for WebSphere Portal v6.0

(do a google search if you do not know how to create a Tag)

Put your custom jar that contains the Tag classes in:
/opt/IBM/WebSphere/PortalServer/shared/app

Put your TLD files in:
/opt/IBM/WebSphere/AppServer/profiles/<profile>/installedApps/<node>/wps.ear/wps.war/WEB-INF/tld

What I do not understand is how the tld is read. I would have expected to have to register the TLD
in some web.xml file, but that is not the case here. So WebSphere has to be doing this in the background somewhere.

Restart your Portal server, and you can now reference your tag from within your theme.

One HUGE drawback of this solution is that you have to restart the Portal server
if you make an update to your TLD or Tag jar and push them out to their
respective locations.