2014-03-31 59 views
2

我已经为我的类声明了一个名为list()的方法,并且为了避免它自己的colliosn/error与PHP自己的list()函数放在一个名称空间中。但不幸的是,我仍然得到错误。她是情景:命名空间和函数名称冲突

我宣布我的文件的开头命名空间:

<?php namespace MyNameSpace\ThisClass; 
use MyNameSpace\ThisClass; 

然后是类:

Class AClass 
{ 
    function list() 
{ 
// something here 
} 
} 

然后实例化它。

$x = MyNameSpace\ThisClass\AClass(); 

我已经宣布在命名空间中的类,并期待它忽略list()与PHP自身list()函数方法的碰撞。但是,PHP抛出以下错误:

syntax error, unexpected 'list' (T_LIST), expecting identifier (T_STRING) 

为什么我仍然得到错误,考虑到我的list()函数嵌套在命名空间的事实?

+0

如何你调用ŧ他'$ class-> list();'方法?同时向我们展示你的'list()'方法定义。 –

+0

这没关系,我已经注释了instanciation,PHP在声明类时抛出PHP。 –

回答

7

由于从PHP Docs

These words have special meaning in PHP. Some of them represent things which look like functions, some look like constants, and so on - but they're not, really: they are language constructs. You cannot use any of the following words as constants, class names, function or method names. Using them as variable names is generally OK, but could lead to confusion.

保留字,如:

  • list()
  • die()
  • include_once()
+0

+1这就是为什么我做了一个脚本,可以找到任何保留字并相应地替换它们。 –

+0

非常感谢 –