2016-07-05 54 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

相关问题