【问题标题】:ZF2 - 1 abstract method and must therefore be declared abstract or implement theZF2 - 1 抽象方法,因此必须声明为抽象或实现
【发布时间】:2017-01-03 09:48:39
【问题描述】:

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

致命错误:类 Blog\Factory\ListControllerFactory 包含 1 抽象方法,因此必须声明为抽象或实现 剩下的方法 (Zend\ServiceManager\Factory\FactoryInterface::__invoke) 在 D:\xampp\htdocs\zend2test\module\Blog\src\Blog\Factory\ListControllerFactory.php 第 28 行

这是我的工厂类:

// 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);
     }
 }

我该如何解决这个问题?

【问题讨论】:

    标签: php zend-framework2


    【解决方案1】:

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

    FactoryInterface extends Factory\FactoryInterface.

    interface 声明了__invoke 方法。因此,要使您的类符合要求,您需要同时实现 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;
    

    在文件的开头(与您的其他“使用”语句一起)

    【讨论】:

    • 感谢@yivi 的回答,但在添加 __invoke() 现在它给出了这个----> Fatal error: Declaration of Blog\Factory\ListControllerFactory::__invoke() must be compatible with Zend\ServiceManager\Factory\FactoryInterface::__invoke(Interop\Container\ContainerInterface $container, $requestedName, array $options = NULL)
    • 很抱歉。立即尝试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    • 2014-07-09
    • 2015-06-29
    • 1970-01-01
    相关资源
    最近更新 更多