phpconfigurationzend-framework2abstract-factoryservicemanager

zend framework 2 abstract factory is printing part of the class code


I'm new on ZF2. I'm trying to reproduce an example of the book learnzf.com which talk about abstract factories. When I try to config a service as AbstractFactoryInterface, ZF2 prints part of the code of the class as result and throw an error like .

module.config.php

    <?php
return array(
    'controllers' => array(
        'invokables' => array(
            'Debug\Controller\Skeleton' => 'Debug\Controller\SkeletonController',
        ),
    ),
    'router' => array(
        'routes' => array(
            'debug' => array(
                'type'    => 'Literal',
                'options' => array(
                    // Change this to something specific to your module
                    'route'    => '/skeleton',
                    'defaults' => array(
                        // Change this value to reflect the namespace in which
                        // the controllers for your module are found
                        '__NAMESPACE__' => 'Debug\Controller',
                        'controller'    => 'Skeleton',
                        'action'        => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    // This route is a sane default when developing a module;
                    // as you solidify the routes for your module, however,
                    // you may want to remove it and replace it with more
                    // specific routes.
                    'default' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => '/[:controller[/:action]]',
                            'constraints' => array(
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                            ),
                            'defaults' => array(
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
    'view_manager' => array(
        'template_path_stack' => array(
            'Debug' => __DIR__ . '/../view',
        ),
    ),
    'service_manager' => array(
        'abstract_factories' => array(
            'Debug\Service\Factory\TimerAbstractFactory',
        ),
    ),
    'timers' => array(
        'timer' => array(
            'times_as_float' => true,
        ),
        'timer_non_float' => array(
            'times_as_float' => false,
        ),
    ),
);

TimerAbstractFactory.php

<?
namespace Debug\Service\Factory;

use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

use Debug\Service\Timer as TimerService;

class TimerAbstractFactory implements AbstractFactoryInterface{

    protected $configKey = 'timers';

    public function canCreateServiceWithName(ServiceLocatorInterface $services,$name,$requestedName){
        $devuelve = false;  
        $config = $services->get('config');
        if(!empty($config)){
            $devuelve = isset($config[$this->configKey][$requestedName]);
        }
        return $devuelve;
    }

    public function createServiceWithName(ServiceLocatorInterface $services,$name,$requestedName){
        $config = $services -> get('config');
        $timer = new TimerService($config[$this->configKey][$requestedName]['times_as_float']);
        return $timer;
    }

}

The result (what I can see in http://localhost:8085):

<body>
get('config');
        if(!empty($config)){
            $devuelve = isset($config[$this-&gt;configKey][$requestedName]);
        }
        return $devuelve;
    }

    public function createServiceWithName(ServiceLocatorInterface $services,$name,$requestedName){
        $config = $services -&gt; get('config');
        $timer = new TimerService($config[$this-&gt;configKey][$requestedName]['times_as_float']);
        return $timer;
    }

} <br>
<font size="1"><table class="xdebug-error xe-fatal-error" dir="ltr" border="1" cellspacing="0" cellpadding="1">
<tbody><tr><th align="left" bgcolor="#f57900" colspan="5"><span style="background-color: #cc0000; color: #fce94f; font-size: x-large;">( ! )</span> Fatal error: Class 'Debug\Service\Factory\TimerAbstractFactory' not found in /var/www/zf/vendor/zendframework/zend-servicemanager/src/ServiceManager.php on line <i>313</i></th></tr>
<tr><th align="left" bgcolor="#e9b96e" colspan="5">Call Stack</th></tr>
<tr><th align="center" bgcolor="#eeeeec">#</th><th align="left" bgcolor="#eeeeec">Time</th><th align="left" bgcolor="#eeeeec">Memory</th><th align="left" bgcolor="#eeeeec">Function</th><th align="left" bgcolor="#eeeeec">Location</th></tr>
<tr><td bgcolor="#eeeeec" align="center">1</td><td bgcolor="#eeeeec" align="center">0.0009</td><td bgcolor="#eeeeec" align="right">230624</td><td bgcolor="#eeeeec">{main}(  )</td><td title="/var/www/zf/public/index.php" bgcolor="#eeeeec">../index.php<b>:</b>0</td></tr>
<tr><td bgcolor="#eeeeec" align="center">2</td><td bgcolor="#eeeeec" align="center">0.0104</td><td bgcolor="#eeeeec" align="right">408576</td><td bgcolor="#eeeeec">Zend\Mvc\Application::init(  )</td><td title="/var/www/zf/public/index.php" bgcolor="#eeeeec">../index.php<b>:</b>22</td></tr>
<tr><td bgcolor="#eeeeec" align="center">3</td><td bgcolor="#eeeeec" align="center">0.0461</td><td bgcolor="#eeeeec" align="right">732368</td><td bgcolor="#eeeeec">Zend\ModuleManager\ModuleManager-&gt;loadModules(  )</td><td title="/var/www/zf/vendor/zendframework/zend-mvc/src/Application.php" bgcolor="#eeeeec">../Application.php<b>:</b>253</td></tr>
<tr><td bgcolor="#eeeeec" align="center">4</td><td bgcolor="#eeeeec" align="center">0.0600</td><td bgcolor="#eeeeec" align="right">829528</td><td bgcolor="#eeeeec">Zend\EventManager\EventManager-&gt;trigger(  )</td><td title="/var/www/zf/vendor/zendframework/zend-modulemanager/src/ModuleManager.php" bgcolor="#eeeeec">../ModuleManager.php<b>:</b>123</td></tr>
<tr><td bgcolor="#eeeeec" align="center">5</td><td bgcolor="#eeeeec" align="center">0.0600</td><td bgcolor="#eeeeec" align="right">829576</td><td bgcolor="#eeeeec">Zend\EventManager\EventManager-&gt;triggerListeners(  )</td><td title="/var/www/zf/vendor/zendframework/zend-eventmanager/src/EventManager.php" bgcolor="#eeeeec">../EventManager.php<b>:</b>205</td></tr>
<tr><td bgcolor="#eeeeec" align="center">6</td><td bgcolor="#eeeeec" align="center">0.0600</td><td bgcolor="#eeeeec" align="right">831336</td><td bgcolor="#eeeeec"><a href="http://www.php.net/function.call-user-func" target="_new">call_user_func</a>
(  )</td><td title="/var/www/zf/vendor/zendframework/zend-eventmanager/src/EventManager.php" bgcolor="#eeeeec">../EventManager.php<b>:</b>444</td></tr>
<tr><td bgcolor="#eeeeec" align="center">7</td><td bgcolor="#eeeeec" align="center">0.0600</td><td bgcolor="#eeeeec" align="right">831616</td><td bgcolor="#eeeeec">Zend\ModuleManager\Listener\ServiceListener-&gt;onLoadModulesPost(  )</td><td title="/var/www/zf/vendor/zendframework/zend-eventmanager/src/EventManager.php" bgcolor="#eeeeec">../EventManager.php<b>:</b>444</td></tr>
<tr><td bgcolor="#eeeeec" align="center">8</td><td bgcolor="#eeeeec" align="center">0.0601</td><td bgcolor="#eeeeec" align="right">840312</td><td bgcolor="#eeeeec">Zend\ServiceManager\Config-&gt;configureServiceManager(  )</td><td title="/var/www/zf/vendor/zendframework/zend-modulemanager/src/Listener/ServiceListener.php" bgcolor="#eeeeec">../ServiceListener.php<b>:</b>222</td></tr>
<tr><td bgcolor="#eeeeec" align="center">9</td><td bgcolor="#eeeeec" align="center">0.0651</td><td bgcolor="#eeeeec" align="right">872296</td><td bgcolor="#eeeeec">Zend\ServiceManager\ServiceManager-&gt;addAbstractFactory(  )</td><td title="/var/www/zf/vendor/zendframework/zend-servicemanager/src/Config.php" bgcolor="#eeeeec">../Config.php<b>:</b>137</td></tr>
</tbody></table></font>
</body>

Solution

  • <? at the start of TimerAbstractFactory.php should be <?php unless you have short tags enabled.