Question

Is there any way to create and use a class named "Array" in PHP? So far, it seems to cause a name conflict with array programming construct, which is not a class.

Was it helpful?

Solution

The class documentation states:

The class name can be any valid label which is not a PHP reserved word.

Array is a reserved word and, thus, you cannot. PHP has no way (AFAIK) to "escape" these words. Also see this answer.

You need to name your class more specifically (like MyArray or AnimalList).

OTHER TIPS

Here is the list of reserved keywords. It's explicitly said :

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.

You've got plenty of alternatives to name your class ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top