-3

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.

Desmond Hume
  • 8,037
  • 14
  • 65
  • 112
  • 4
    Could you explain **why** you **need** to create a class named Array? Also: did you see [this answer](http://stackoverflow.com/questions/11792070/php-reserved-words-as-namespaces-and-class-names)? – RobIII Jan 07 '14 at 22:53
  • 1
    no. you can't. it'd be like trying to create a class named `new`. e.g. `$new = new New;`. – Marc B Jan 07 '14 at 22:53
  • You can not use platform constants. You could use MyArray, hehahaah. Many other API's create their own wrappers, like ArrayCollection, etc – Rottingham Jan 07 '14 at 22:54
  • List of reserved keywords http://www.php.net/manual/en/reserved.keywords.php – Paul Jan 07 '14 at 22:55
  • As you said, it's a `construct`, and hence you cannot. For the same reason you cannot have an `if` or `unset` class. – Wrikken Jan 07 '14 at 22:55
  • Wouldn't [`ArrayObject`](http://php.net/ArrayObject) suit any use case? Explain your use case. (And yes, there would be a way to shoehorn a classname of "array", and an even more cumbersome way to instantiate it..) – mario Jan 07 '14 at 22:59
  • @DesmondHume ok, keep avoiding answering legitimate questions (like: why would you want this in the first place?) – RobIII Jan 07 '14 at 23:02
  • I needed `Array` class for an extremely interesting purpose, but I changed my mind about telling which one. – Desmond Hume Jan 07 '14 at 23:03
  • @DesmondHume Please DO elaborate. I don't see why you wouldn't (or can't) tell us and enlighten us? – RobIII Jan 07 '14 at 23:05
  • @RobIII Prejudice and enlightenment ain't very compatible.. – Desmond Hume Jan 07 '14 at 23:07
  • @DesmondHume I don't see why you seem so offended? Didn't I ask a normal question? By the way: there *might* be a way (using runtime code generation) but, even if it worked, it would cause more problems that it would (probably) solve. That's why it's important you tell us why you need this in the first place. – RobIII Jan 07 '14 at 23:09

2 Answers2

3

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).

Community
  • 1
  • 1
RobIII
  • 8,488
  • 2
  • 43
  • 93
1

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 ;)

Community
  • 1
  • 1
Maen
  • 10,603
  • 3
  • 45
  • 71