2014-03-31 62 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

非常感謝 –