Symfony2 Use PHP Class Constant in YAML Config? Symfony2 Use PHP Class Constant in YAML Config? symfony symfony

Symfony2 Use PHP Class Constant in YAML Config?


For Symfony >=2.4 you can use expression language

Example:

'@=constant("Symfony\\Bridge\\Monolog\\Logger::INFO")'


As of Symfony 3.2, it's also possible to use PHP constants in YAML files using a special !php/const: syntax:

parameters:    bar: !php/const:PHP_INT_MAX

See the Symfony blog post for more details.


Please note: In Symfony 4 the syntax was slightly changed. You have to use a space instead of a double colon, for example:

parameters:    bar: !php/const PHP_INT_MAX

Otherwise, you will get a FileLoaderLoadException with the message "Notice: Uninitialized string offset".


In versions before Symfony 3.2, injecting PHP-constants only works with XML:

<parameter key="my_service.my_const" type="constant">My\Bundle\DependencyInjection\MyClass::MY_CONST</parameter>

If you want to keep yor yml files, you could just import the xml-file into your services.yml. Mixing config styles might be a bit ugly, but as far as I know this is the only way to do it.

If this doesn't work for you, my comment to your question applies.