tags:

views:

342

answers:

3

I have one PHP5 object passing messages to another, and would like to attach a type to each message. For example, MSG_HOT, MSG_WARM, and MSG_COLD. If PHP5 had an enum type, I would probably use that to define the message types, but (unless I'm mistaken) there is no such animal. I've looked at a few options:

Strings ('MSG_HOT', 'MSG_WARM', and 'MSG_COLD') are bad because I would inevitably type something like 'MSG_WRAM' and things would break. Numbers suffer the same problem and are also less clear.

Defines work:

define('MSG_HOT', 1);
define('MSG_WARM', 2);
define('MSG_COLD', 3);

but pollute the global namespace, and thus would require more verbose names to ensure uniqueness. I'd prefer not to have my code littered with things like APPLICATIONNAME_MESSAGES_TYPE_HOT.

Finally, I could use class names to distinguish types, like so:

class MessageHot extends Message {}
class MessageWarm extends Message {}
class MessageCold extends Message {}
class Message
{
    public function Type()
    {
        return get_class($this);
    }
    public function Data()
    {
        return $this->data;
    }
    public function __construct($data)
    {
        $this->data = $data;
    }
    private $data;
}

This is good, I think, but is also a lot of work for what seems like it ought to be a simple concept.

Am I missing a better alternative?

A: 

You could use class constants:

class Message
{
    const hot = 0;
    const warm = 1
    const cold = 2;
}

foo(Message::hot);
Greg
Heh, I swear this wasn't on the page when I answered. Greg, most PHP (PEAR,Zend, etc.) coding standards require the use of ALL CAPS for constant names. It certainly helps with code recognition.
hobodave
+5  A: 

A very common convention is to use class constants in PHP.

e.g.

class Message
{
    const HOT  = 0;
    const WARM = 1;
    const COLD = 2;
}
hobodave
You would use this like this: `Message::HOT`. I also believe that `Message` should be declared as `abstract`.
philfreo
+2  A: 

I also use a class with consts for this. I add the following

  • make the class abstract, to make sure no one tries to instantiate it
  • add a static array that maps the consts to strings for printing friendly messages
  • implement a static __toString() method to do the previous

    abstract class Message {
        const HOT = 0;
        const WARM = 1;
        const COLD = 2;
    
    
    
    public static $enums= array(
        self::HOT => "hot",
        self::WARM => "warm",
        self::COLD => "cold"
    );
    
    
    public static __toString($enum) {
        return self::$enums[$enum];
    }
    
    }

I can also use the Message::$enums to test variables:

if (!array_key_exists($is_it_valid, Message::$enums)
grantwparks
Found that using the __toString for this is ill-advised, as newer PHP versions than I was using are complaining about it. So, might be best to use a different name for this functionality.
grantwparks