vendredi 16 novembre 2018

App architecture - When to use a separate class for an object's property values?

Let's stay that you have 2 objects like the following:

class State {
    const FRIENDLY = 1;
    const ANGRY = 2;
    const SAD = 3;
}

class Dog
{
    public $state_id;
}

// Usage
$dog = new Dog();
$dog->state_id = State::FRIENDLY;

Note: The State object may get more values but it's very unlikely.

The State object will only be used with the Dog object (see usage examples).

Would you write the code like above or like the following:

class Dog
{
    const STATE_FRIENDLY = 1;
    const STATE_ANGRY = 2;
    const STATE_SAD = 3;

    public $state_id;
}

// Usage
$dog = new Dog();
$dog->state_id = Dog::STATE_FRIENDLY;

Please explain your choice.

Aucun commentaire:

Enregistrer un commentaire