ZF2-1 抽象方法,因此必须声明为抽象方法或实现

ZF2 - 1 abstract method and must therefore be declared abstract or implement the

你好,我正在尝试 运行 This Blog Example 所以我已经完成了本教程所说的每一个步骤,但现在我收到了这个错误:创建工厂后 Class

Fatal error: Class Blog\Factory\ListControllerFactory contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Zend\ServiceManager\Factory\FactoryInterface::__invoke) in D:\xampp\htdocs\zend2test\module\Blog\src\Blog\Factory\ListControllerFactory.php on line 28

这是我的工厂class:

// Filename: /module/Blog/src/Blog/Factory/ListControllerFactory.php
 namespace Blog\Factory;

 use Blog\Controller\ListController;
 use Zend\ServiceManager\FactoryInterface;
 use Zend\ServiceManager\ServiceLocatorInterface;

 class ListControllerFactory implements FactoryInterface
 {

    private $serviceLocator;
     /**
      * Create service
      *
      * @param ServiceLocatorInterface $serviceLocator
      *
      * @return mixed
      */
     public function createService(ServiceLocatorInterface $serviceLocator)
     {
         $realServiceLocator = $serviceLocator->getServiceLocator();
         $postService        = $realServiceLocator->get('Blog\Service\PostServiceInterface');

         return new ListController($postService);
     }
 }

我该如何解决这个问题?

您正在使用的 FactoryInterface 扩展了另一个接口:

FactoryInterface extends Factory\FactoryInterface.

并且 interface 声明了 __invoke 方法。因此,为了使您的 class 符合要求,您需要同时实施 createService__invoke.

同时声明 __invoke 方法。例如

public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        // get your dependency
        $postService = $container->get('Blog\Service\PostServiceInterface');
        // inject it int the constructor
        return new ListController($postService);
    }

此外,添加行:

use Interop\Container\ContainerInterface;

在文件的开头(与其他 "use" 语句)