如何在类中使用内置函数名称作为方法名称
问题描述:
我有一个名为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){
}
}
答
不能,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://stackoverflow.com/questions/5298507/can-i-use-php-reserved-names-for-my-functions-and-classes – Saty
无法使用内置函数。 –