Symfony2 Use PHP Class Constant in YAML Config?

后端 未结 6 1800
梦如初夏
梦如初夏 2020-12-15 05:45

I know this is probably not possible, but is there a clean way to use a PHP class constant within a YAML config/services/etc. file for Symfony2?

For example, if I ha

6条回答
  •  -上瘾入骨i
    2020-12-15 06:14

    It should be possible to insert argument as plain 'text' in config and inside class __construct($constant1) then request constant through variable by constant($constant1) function.

    But I am not sure about global constants here, because they may not be defined at time of using, but it shouldn't be a problem for class constants with whole namespace, because namespace locator is already defined at moment of calling the class.

    Example config:

    services:
        my_service:
            class: Some\Class
        arguments:
            - 'My\Bundle\DependencyInjection\MyClass::MY_CONST'
    

    and example class method:

    public function __construct($arg1)
    {
        $myRequiredConstantValue = constant($arg1);
    }
    

提交回复
热议问题