PHP reserved words as namespaces and class names

拟墨画扇 提交于 2019-11-28 13:57:06

PHP will throw an error like:

Parse error: syntax error, unexpected T_ARRAY, expecting T_STRING

if you try:

class Array
{

}
$myClass = new Array;

Instead, try something like

class myArray
{

}
$myClass = new myArray;

[edit] I'll elaborate on that a little, they're reserved for a reason because in the first scenario above, PHP wouldn't be able to tell the difference between you defining an array or initialising a class of the same name, so it throws an error. There's no way round this like in MySQL, for example, where you can escape reserved words with a backtick. So in PHP you're forced to change the name, you don't have to change it much, one char will do as long as you're not using the exact same name as a reserved word.

Hope that helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!