简体   繁体   English

PHP数组,我可以定义自己的Array类吗?

[英]PHP Arrays, Can I define my own Array class?

I can see in PHP 5.3.2 there is an ArrayObject class. 我可以在PHP 5.3.2中看到一个ArrayObject类。 Is it possible to declare a new object named Array 是否可以声明一个名为Array的新对象

that extends the ArrayObject . 扩展了ArrayObject But Im not sure if ' Array ' is a reserved keyword or should I use my own prefix ie XArray(), MyArray etc... 但是我不确定' Array '是保留关键字还是我应该使用自己的前缀,即XArray(),MyArray等...

What I would like to have is 我想拥有的是

class Array extends ArrayObject {

...my own code

}

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. 您需要使用其他名称。

If it works right now, it may just happen that they do create an Array class in the future. 如果现在Array ,则将来可能会创建一个Array类。 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. 看到他的回答让我意识到了为什么它不起作用: array是php中的关键字(允许您创建数组文字),而关键字在php中不区分大小写。

Im not sure if 'Array' is a reserved keyword 我不确定'Array'是否为保留关键字

That's pretty easy to check. 这很容易检查。 PHP's list of reserved keywords has "array()" listed front and center. PHP的保留关键字列表的前面和中间列出了“ array()”。

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. 您需要将您的类重命名为其他名称,并且您的名称不仅要区分大小写,还必须区分大小写,因为PHP类名不区分大小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM