duanhe1965 2017-05-27 17:15
浏览 240
已采纳

命名空间中允许的PHP保留关键字 (公共,私人,默认)

PhpStorm is highlighting the following namespace as an error.

<?php

namespace App\Http\Controllers\Public;

Error: Expected: identifier

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

  • 写回答

1条回答 默认 最新

  • doudong4532 2017-05-28 09:10
    关注

    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.

    List of reserved keywords, which you can't use for namespacing:

    __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

    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)

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?