名称空间中允许使用PHP保留关键字? (公开,私有,默认) [英] PHP reserved keywords allowed in namespace? (public, private, default)

查看:106
本文介绍了名称空间中允许使用PHP保留关键字? (公开,私有,默认)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PhpStorm将以下名称空间突出显示为错误.

PhpStorm is highlighting the following namespace as an error.

<?php

namespace App\Http\Controllers\Public;

错误:Expected: identifier

通常.名称间隔不接受publicfunctionclass之类的保留关键字吗?

In general. Are reserved keywords like public, function, class not acceptable for namespacing?

推荐答案

PHP手册提示,从PHP 7.0.0开始,这些关键字可以用作类,接口和特征的属性,常量和方法名称,除了不能将类用作常量名称外,合并请求的关键字public.

The PHP manual hints that as of PHP 7.0.0 these keywords are allowed as property, constant, and method names of classes, interfaces and traits, except that class may not be used as constant name, which also incorporate the requested keyword public.

保留的关键字列表,您不能将其用于命名空间:

__halt_compilerabstractandarrayasbreakcallablecasecatchclasscloneconstcontinuedeclaredefaultdiedoechoelseelseifemptyenddeclareendforendforeachendifendswitchendwhileevalexitextendsfinalforforeachfunctionglobalgotoifimplementsincludeinclude_onceinstanceofinsteadofinterfaceissetlistnamespaceneworprintprivateprotectedpublicrequirerequire_oncereturnstaticswitchthrowtraittryunsetusevarwhilexor

__halt_compiler, abstract, and, array, as, break, callable, case, catch, class, clone, const, continue, declare, default, die, do, echo, else, elseif, empty, enddeclare, endfor, endforeach, endif, endswitch, endwhile, eval, exit, extends, final, for, foreach, function, global, goto, if, implements, include, include_once, instanceof, insteadof, interface, isset, list, namespace, new, or, print, private, protected, public, require, require_once, return, static, switch, throw, trait, try, unset, use, var, while, xor

解决此问题的一种可能的解决方法是在单词中添加一个额外的字符使用单词的复数形式.另一个机会是在名称空间中上一层并使用关键字作为前缀 PublicFooController. (或后缀,如果您愿意的话)

A potential workaround to fix this issue is to add an extra character to the word or use the plural form of the word. Another opportunity would be to go one level up in the namespace and use the keyword as a prefix PublicFooController. (or even suffix, if you prefer that)

我的意图是将我的Web应用程序划分为public(用于访客和访客)和private(用于私人需求)区域.
我选择了namespace App\Http\Controllers\Frontendnamespace App\Http\Controllers\Backend,而不是尝试将单词publicprivate并入我的命名空间,即使我个人认为这些通常更适合我的需求,因为两者都有前端和后端.但是...无论如何,当我使用publicprivate时,这使我不必麻烦地组织文件和名称空间.

My intention was to scope my web app into public (for guests and visitors) and private (for private needs) area.
I went for namespace App\Http\Controllers\Frontend and namespace App\Http\Controllers\Backend instead of trying to incorperate the words public and private in to my namespacing, even if I personally think these generally fits better to my needs, because both have frontend and backend. But... Anyway, this keeps me off having trouble organizing my files and namespaces the way I would do, when I would use public and private.

这篇关于名称空间中允许使用PHP保留关键字? (公开,私有,默认)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆