未找到Autoload PSR-4命名空间界面

时间:2017-07-01 15:27:49

标签: php namespaces composer-php

{ 
       "autoload": {

       "psr-4": {
        "Acme\\": "src"
    }
}
}

我的composer.json文件

namespace Acme;

Interface Responds
{   
    public function userRegisteredSuccessfuly();

    public function userRegisteredunSuccessfuly();  
}

接口位于C:\ xampp \ htdocs \ bootcamp \ src \ respondstouserregistration.php

<?php 
namespace Acme;

class RegisterUser
{
    public function execute(array $data, Responds $listener) //data 
    {
        var_dump('registering the user.');
        $listener->userRegisteredSuccessfuly();
    }
}

在我引用的RegisterUser中,接口响应,registeruser也位于src

<?php 
namespace Acme;

class AuthController implements Responds
{
    protected $registration;

    public function __construct(RegisterUser $registration)
    {
        $this->registration = $registration;
    }

    public function register()
    {
        $this->registration->execute([], $this);
    }

    public function userRegisteredSuccessfuly()
    {
        var_dump('created successfuly. redirect somewhere.');
    }


    public function userRegisteredunSuccessfuly()
    {
        var_dump('created unsuccessfuly. redirect back.');
    }

}

在本课程中,我实现了响应界面。

AuthController也位于C:\ xampp \ htdocs \ bootcamp \ src \ authcontroller.php

现在我的错误是

  

PHP致命错误:在C:\ xampp \ htdocs \ bootcamp \ src \ authcontroller.php中找不到接口'Acme \ Responds'   如何让Authcontroller找到接口所在的php文件?

1 个答案:

答案 0 :(得分:3)

PSR-4自动加载假定您的类名与文件名匹配。因此,您的Interface Responds应该位于responds.php文件中,而不是respondstouserregistration.php文件中。重命名文件,或将界面重命名为Interface Respondstouserregistration