2016-07-05 55 views
1

我有一個名爲Validation的類和名爲array的方法。由於array在PHP中的內置函數及其引發錯誤如何在類中使用內置函數名稱作爲方法名稱

Parse error: syntax error, unexpected 'array' (T_ARRAY), expecting identifier (T_STRING) in D:\xampp\htdocs\mvc\app\validation.php on line 82

有沒有一種方法,我可以用數組作爲方法的名字嗎?

//This is a class 
class Validation extends Controller{ 

    //This is a method 
    public function array($field, $name){ 

    } 

} 
+5

檢查http://stackoverflow.com/questions/5298507/can-i-use-php-reserved-names-for-my-functions-and-classes – Saty

+2

無法使用內置函數。 –

回答

2

不能,PHP沒有轉義保留字的方法,以便它們可以用作類/方法/ etc名稱。

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.

As of PHP 7.0.0 these keywords are allowed as as property, constant, and method names of classes, interfaces and traits, except that class may not be used as constant name.

查看http://php.net/manual/en/reserved.keywords.php查看完整列表。

演示:https://3v4l.org/WVgvh

相關問題