1

I have a class called Validation and a method named array. Since array is a builtin function in PHP its throwing an error

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

Is there a way I can use array as a method name?

//This is a class
class Validation extends Controller{

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

    }

}
Red Virus
  • 1,633
  • 3
  • 25
  • 34

1 Answers1

2

Nope, PHP doesn't have a way of escaping reserved words so that they can be used as class/method/etc names.

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.

See http://php.net/manual/en/reserved.keywords.php for a full list.

Demo: https://3v4l.org/WVgvh

Community
  • 1
  • 1
iainn
  • 16,826
  • 9
  • 33
  • 40