Php 为什么symfony容器有重复的服务?

Php 为什么symfony容器有重复的服务?,php,symfony,symfony4,Php,Symfony,Symfony4,我想将classApp\Service\SomeService注册为服务 这是我的服务。yaml: services: _defaults: autowire: false autoconfigure: true public: true App\: resource: '../src/*' exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'

我想将class
App\Service\SomeService
注册为服务

这是我的
服务。yaml

services:
    _defaults:
        autowire: false
        autoconfigure: true
        public: true
    App\:
        resource: '../src/*'
        exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'
    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']
    someservice:
        class: App\Service\SomeService
现在我运行
debug:container someservice

Information for Service "someservice"
=====================================

 ---------------- ------------------------- 
  Option           Value                    
 ---------------- ------------------------- 
  Service ID       someservice              
  Class            App\Service\SomeService  
  Tags             -                        
  Public           yes                      
  Synthetic        no                       
  Lazy             no                       
  Shared           yes                      
  Abstract         no                       
  Autowired        no                       
  Autoconfigured   yes                      
 ---------------- ------------------------- 
Information for Service "App\Service\SomeService"
=================================================

 ---------------- ------------------------- 
  Option           Value                    
 ---------------- ------------------------- 
  Service ID       App\Service\SomeService  
  Class            App\Service\SomeService  
  Tags             -                        
  Public           yes                      
  Synthetic        no                       
  Lazy             no                       
  Shared           yes                      
  Abstract         no                       
  Autowired        no                       
  Autoconfigured   yes                      
 ---------------- ------------------------- 
而且,当我运行
debug:container-App\\Service\\SomeService
时:

Information for Service "someservice"
=====================================

 ---------------- ------------------------- 
  Option           Value                    
 ---------------- ------------------------- 
  Service ID       someservice              
  Class            App\Service\SomeService  
  Tags             -                        
  Public           yes                      
  Synthetic        no                       
  Lazy             no                       
  Shared           yes                      
  Abstract         no                       
  Autowired        no                       
  Autoconfigured   yes                      
 ---------------- ------------------------- 
Information for Service "App\Service\SomeService"
=================================================

 ---------------- ------------------------- 
  Option           Value                    
 ---------------- ------------------------- 
  Service ID       App\Service\SomeService  
  Class            App\Service\SomeService  
  Tags             -                        
  Public           yes                      
  Synthetic        no                       
  Lazy             no                       
  Shared           yes                      
  Abstract         no                       
  Autowired        no                       
  Autoconfigured   yes                      
 ---------------- ------------------------- 
因此,我有另一个指向同一类的服务:

namespace App\Controller;

use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use App\Service\SomeService;

class DefaultController extends Controller
{
    /**
     * @Route("/")
     * @return Response
     */
    public function index()
    {
        var_dump($this->get('someservice') === $this->get(SomeService::class));
        return new Response;
    }
}
产出:

布尔(假)


为什么我要注册两个服务而不是一个服务?

这是因为您在
服务中注册了两次。yaml

使用此行自动执行一次:

App\:
    resource: '../src/*'
    exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'
另一种是手动:

someservice:
    class: App\Service\SomeService
因为它们的名称不同(
someservice
App\Service\someservice
),所以Symfony的名称不同

我建议您删除第二个声明,并仅在控制器中使用其完全限定名调用服务:
$this->get(SomeService::class)