Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony2两个容器服务实例_Php_Symfony - Fatal编程技术网

Php Symfony2两个容器服务实例

Php Symfony2两个容器服务实例,php,symfony,Php,Symfony,我有一个API服务,我有两个独立的帐户登录。我需要使用该类的克隆实例,并通过accountname参数调用它 在symfony之前,我为每个类创建了一个单例并将其存储在一个数组中 array(["account1"]=>api instance, ["account2"]=>api instance) 类登录如下所示 class api { protected $ses_time; protected $accounts = array(

我有一个API服务,我有两个独立的帐户登录。我需要使用该类的克隆实例,并通过accountname参数调用它

在symfony之前,我为每个类创建了一个单例并将其存储在一个数组中

array(["account1"]=>api instance, ["account2"]=>api instance)
类登录如下所示

class api
{
    protected $ses_time;
    protected $accounts = array(
                "account1"=>array(
                        "username"=>"username",
                        "password"=>"password"
                ),
                "account2"=>array(
                        "username"=>"username",
                        "password"=>"password"
                )
            );

    public function login($account)
    {
        $username = $this->accounts[$account]["username"];
        $password = $this->accounts[$account]["password"];

         $this->soap->login(
                array(
                        'username' => $username,
                        'password' => $password,
                        'session_duration' => $this->ses_time
                )
        );
    }
}
将这些存储在服务容器中会非常有用,问题是如果我将其创建为一个服务,我不能两次实例化该类,也不能将dynamic
$account
参数传递给要登录的服务


如果我将范围更改为
prototype
它不再是一个单例

您可以实现一个
多例
,而不是每个帐户键的单例,我可以看到这将如何工作,而不是处理一系列单例,但这并没有使用symfony服务容器Symfony2容器用于
依赖关系管理
,如果您计划将类注入其他类(
服务
),那么将其作为服务使用是有意义的,否则您可以作为单个类使用。