Suppose I have the following Doctrine 2 object:
class User {
protected $id;
protected $name;
protected $status;
}
A user can have several statuses, for example: Waiting, Active, Hanging. These statuses are required throughout the code (services, repositories, etc.), as well as at the user interface level (the user editing form will display them in a drop-down list).
To avoid defining them in several places, what I have done so far has been to use a class to store them (all application constants) and looks something like this:
class App_Constants extends Zrzr_Constants
{
protected static $_constants = array(
'users' => array(
'status' => array(
0 => 'Pending',
1 => 'Active',
2 => 'Suspended' ) ) );
}
The base class (Zrzr_Constants) will offer some methods to extract them, and it looks like this:
class Zrzr_Constants
{
protected static $_constants = array();
public static function getConstantValues( $key, $subkey )
{
}
public static function getConstantByName( $name )
{
}
}
General use:
$pendingStatus = App_Constants::getConstantByName( 'USERS.STATUS.PENDING' );
$statuses = App_Constants::getConstantValues('users', 'status');
Of course, this means that there are some limitations in that constant labels cannot contain periods, but I can live with it.
Doctrine 2 DDD-, , , "" " " ( Doctrine 2 ), , , , ( const).
, , UI? ( ) ().