2017-01-03 61 views
1

你好,我試圖運行This Blog Example所以我有本教程所說的每一個步驟,但現在我得到這個錯誤:創建工廠類後ZF2 - 1抽象方法,因此必須聲明爲抽象或實現

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

這裏是我的工廠類:

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

如何解決這個問題?

回答

2

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; 

在你的文件的開頭(與其他 「使用」 的語句)

+0

感謝@yivi的答案,但加入後** __調用( )**現在它賦予這個---->'致命錯誤:聲明Blog \ Factory \ ListControllerFactory :: __ invoke()必須與Zend \ ServiceManager \ Factory \ FactoryInterface :: __ invoke兼容(Interop \ Container \ ContainerInterface $ container ,$ requestedName,array $ options = NULL)' –

+0

對不起。現在就試試。 – yivi

+0

我已經試過,但是同樣的錯誤 –

相關問題