1

I can see in PHP 5.3.2 there is an ArrayObject class. Is it possible to declare a new object named Array

that extends the ArrayObject. But Im not sure if 'Array' is a reserved keyword or should I use my own prefix i.e XArray(), MyArray etc...

What I would like to have is

class Array extends ArrayObject {

...my own code

}
IEnumerable
  • 3,610
  • 14
  • 49
  • 78

3 Answers3

9

All you'd have to do is try to run:

<?php class Array {}

And you'd see:

Parse error: syntax error, unexpected 'Array' 

So the answer is, no, you cannot. You'll need to use a different name.

Matthew
  • 47,584
  • 11
  • 86
  • 98
1

If it works right now, it may just happen that they do create an Array class in the future. It is best just to stay away from names that are so obviously logical for the language to define.

edit: As Matthew pointed out, it doesn't work. Seeing his answer made me realize why it doesn't work: array is a keyword in php (allowing you to make an array literal) and keywords are case insensitive in php.

Jasper
  • 11,590
  • 6
  • 38
  • 55
0

Im not sure if 'Array' is a reserved keyword

That's pretty easy to check. PHP's list of reserved keywords has "array()" listed front and center.

That page also lists the following guidance:

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.

So no, you can't. You need to rename your class to something else, and your name must be differentiated by more than just letter-casing since PHP class names are case-insensitive.

Farray
  • 8,290
  • 3
  • 33
  • 37
  • I knew that keywords are case insensitive (thus creating a problem here), but are class names case insensitive as well? – Jasper Feb 05 '12 at 01:34
  • @Jasper That is correct. There is a [change request](https://bugs.php.net/bug.php?id=26575&edit=1) asking for case-sensitive class names and it's status is "wont fix". See also http://stackoverflow.com/questions/5260168/capital-letters-in-class-name-php – Farray Feb 05 '12 at 01:37