- 先还原下事故现场:
FrontAssetLoad.php 编码格式: utf-8 Bom
<?php
namespace myproject;
header('Content-Type: application/json;charset=utf-8');
class FrontAssetLoad
{
public function index()
{
echo "这是前端资源加载器";
}
}
index.php 编码: ANSI格式
<?php
namespace myproject;
include_once "./FrontAssetLoad.php";
use myproject;
$obj = new FrontAssetLoad();
$obj->index();
运行结果:
Fatal error: Namespace declaration statement has to be the very first statement in the script in H:\dev-www\myproject\FrontAssetLoad.php on line 2
总是报错,苦恼了好半天,最后把所有的文件编码改成: utf-8 格式
运行结果:
这是前端资源加载器
总结教训: 以后php文件编码都有统一为utf-8格式,utf-8格式,utf-8格式。 重要的事情讲三遍看看自己能不能长心,可以把note pad++ 和 sublime 的文件编码统一设置为utf-8格式。