Fatal error: Namespace declaration statement has to be the very first statement or after any declare call in the script in


學習php的命名空間,直接把手冊的代碼粘貼過來,卻報錯了:Fatal error: Namespace declaration statement has to be the very first statement or after any declare call in the script in

代碼如下:

 1 <?php 
 2 namespace my\name; // 參考 "定義命名空間" 小節
 3 
 4 
 5 class MyClass {}
 6 function myfunction() {}
 7 const MYCONST = 1;
 8 
 9 $a = new MyClass;
10 $c = new \my\name\MyClass; // 參考 "全局空間" 小節
11 
12 $a = strlen('hi'); // 參考 "使用命名空間:后備全局函數/常量" 小節
13 
14 $d = namespace\MYCONST; // 參考 "namespace操作符和__NAMESPACE__常量" 小節
15 
16 $d = __NAMESPACE__ . '\MYCONST';
17 echo constant($d); // 參考 "命名空間和動態語言特征" 小節

解決辦法:
將頁面轉為utp-8無dom格式就OK了。以notepad++為例:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM