首页 >后端开发 >php教程 >php命名空间报错

php命名空间报错

WBOY
WBOY原创
2016-09-21 14:13:131727浏览

报错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