Useful for storing global constants that will not change between environments and therefore should not really be held in the parameters.yml

Constants

<?php
namespace AppBundle\Util;

class Constants
{
    public function getDateFormat() 
    {
        return 'd/m/Y, h:i';
    }
}

ConstantsAwareTrait

A trait to allow setting of the constants property.

<?php
namespace AppBundle\Util;

trait ConstantsAwareTrait
{
    /**
     * The Constants instance.
     *
     * @var Constants
     */
    public $constants;

    /**
     * Sets Constants.
     *
     * @param Constants $constants
     */
    public function setConstants(Constants $constants)
    {
        $this->constants = $constants;
    }
}

DateFormatter

An example class that could use the constants.

<?php
namespace AppBundle\Util;

class DateFormatter
{
    use ConstantsAwareTrait;
    
    public function getFormattedDate(int $timestamp)
    {
        return date($timestamp, $this->constants->getDateFormat());
    }
}

Service Configuration

# app/services.yml
services:
  app.util.date_formatter:
    class: AppBundle\Util\DateFormatter
    calls:
      - ['setConstants', ['@app.constants']]