首頁 >後端開發 >php教程 >php命名空間報錯

php命名空間報錯

WBOY
WBOY原創
2016-09-21 14:13:131691瀏覽

報錯Parse error: syntax error, unexpected T_STRING in F:appwampserverAppServwwwnamespace.php on line 3

<code>
<?php

namespace Article;
const PATH = '/article';
function getCommentTotal() {
    return 100;
}
class Comment { }

namespace MessageBoard;
const PATH = '/message_board';
function getCommentTotal() {
    return 300;
}
class Comment { }

//调用当前空间的常量、函数和类
echo PATH; ///message_board
echo getCommentTotal(); //300
$comment = new Comment();

//调用Article空间的常量、函数和类
echo \Article\PATH; ///article
echo \Article\getCommentTotal(); //100
$article_comment = new \Article\Comment();
?>
</code>

回覆內容:

報錯Parse error: syntax error, unexpected T_STRING in F:appwampserverAppServwwwnamespace.php on line 3

<code>
<?php

namespace Article;
const PATH = '/article';
function getCommentTotal() {
    return 100;
}
class Comment { }

namespace MessageBoard;
const PATH = '/message_board';
function getCommentTotal() {
    return 300;
}
class Comment { }

//调用当前空间的常量、函数和类
echo PATH; ///message_board
echo getCommentTotal(); //300
$comment = new Comment();

//调用Article空间的常量、函数和类
echo \Article\PATH; ///article
echo \Article\getCommentTotal(); //100
$article_comment = new \Article\Comment();
?>
</code>

const 關鍵字只能用來定義類別內常數,類別外請使用 define。 (PHP 5.3 之前)

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn