kgendig
kgendig

Reputation: 13

Zf 3 Introducing Services and the ServiceManager

I'm getting error:

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 /d0/home/kgendig/www/Zend/module/Blog/src/Blog/Factory/ListControllerFactory.php on line 28

I'm doing all with https://framework.zend.com/manual/2.4/en/in-depth-guide/services-and-servicemanager.html

What i have to change, my zend_version(); is 2.6.0

<?php
// 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
{
    /**
     * 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);
    }
}

Upvotes: 1

Views: 633

Answers (1)

Al Foиce    ѫ
Al Foиce ѫ

Reputation: 4315

Your ListControllerFactory class implements the FactoryInterface. So you have to define all abstract functions of this interface in your class. Here, your FactoryInterface needs the __invoke() method (check how you call it), so you have to define it in your ListControllerFactory.

It seems you are mixing ZF2/3 components. In ZF3 FactoryInterface code (see here), you have instructions for upgrading from V2 to V3:

If upgrading from v2, take the following steps:

  • rename the method createService() to __invoke(), and:
  • rename the $serviceLocator argument to $container, and change the typehint to Interop\Container\ContainerInterface
  • add the $requestedName as a second argument
  • add the optional array $options = null argument as a final argument
  • create a createService() method as defined in this interface, and have it proxy to __invoke().

This describe how to solve your problem, but maybe there are several similar issues in your code. Try not mixing ZF2.4 and ZF3 component. Don't use dev-master in your composer.json. I suggest you to use only ZF2 component and ZF2 tutorial, or, if you want to learn ZF3, only ZF3 components and the ZF3 tutorial.

Upvotes: 1

Related Questions