ZF2中我的module.php中的命名空间错误

问题描述:

Error :

Fatal error: Namespace declaration statement has to be the very first statement in the script in C:\xampp\htdocs\zf2-tutorial\module\Album\Module.php on line 8

Module.php

<?php

namespace Application;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;

class Module
{
public function onBootstrap(MvcEvent $e)
{
    $eventManager        = $e->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);
}

public function getConfig()
{
    return include __DIR__ . '/config/module.config.php';
}

public function getAutoloaderConfig()
{
    return array(
        'Zend\Loader\StandardAutoloader' => array(
            'namespaces' => array(
                __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
            ),
        ),
    );
  }
}

I've already checked for white spaces and as you can see namespace declaration is first after the opening php tag. Can't figure out what's causing this.

错误: p>

 致命错误:命名空间声明语句必须 是第8行中C:\ xampp \ htdocs \ zf2-tutorial \ module \ Album \ Module.php中脚本中的第一个语句
  code>  pre> 
 
 

Module.php p>

 &lt;?php 
 
namespace Application; 
 
use Zend \ Mvc \ ModuleRouteListener; 
use Zend \ Mvc \ MvcEvent; 
 
class Module 
 {  
公共函数onBootstrap(MvcEvent $ e)
 {
 $ eventManager = $ e-&gt; getApplication() - &gt; getEventManager(); 
 $ moduleRouteListener = new ModuleRouteListener(); 
 $ moduleRouteListener-&gt; attach  ($ eventManager); 
} 
 
公共函数getConfig()
 {
返回包括__DIR__。  '/config/module.config.php'; 
} 
nn公共函数getAutoloaderConfig()
 {
返回数组(
'Zend \ Loader \ StandardAutoloader'=&gt;数组(
'名称空间'=  &gt;数组(
 __NAMESPACE__ =&gt; __DIR __。'/ src /'。_ _ NASMESPACE __,
),
),
); 
} 
} 
  code>  pre> 
  
 

我已经检查了空格,你可以看到命名空间声明是在打开php标签之后的第一个。 无法弄清楚造成这种情况的原因。 p> div>

That error is telling you that there are any output before the namespace declaration.

Look for whitespaces before <?php in your files. This could be caused by some editors adding BOM to UTF8 files, so you should save them as "UTF8 without BOM" or "Unicode".