gpt4 book ai didi

PHP命名空间 'Public'

转载 作者:行者123 更新时间:2023-12-04 20:19:08 24 4
gpt4 key购买 nike

在Laravel 4应用程序中,是否可以在名为Public的命名空间中创建 Controller ?像这样:

<?php namespace Public;

class MyController extends \BaseController {

}

这样做给我一个错误:

syntax error, unexpected 'Public' (T_PUBLIC), expecting identifier (T_STRING) or \ (T_NS_SEPARATOR) or '{'



但是,如果我将 namespace 更改为 PublicControllers,则可以正常工作。这是否意味着 Public是不能用作 namespace 的保留字?

最佳答案

public is a reserved word in PHP:

These words have special meaning in PHP. Some of them represent things which look like functions, some look like constants, and so on - but they're not, really: they are language constructs. 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.



虽然这里没有特别提到 namespace ,但我们可以查看 the PHP grammar并看到 namespace 应由 T_STRING结合在一起的 T_NS_SEPARATOR组成(反斜杠)。由于 public具有其自己的 token 类型( T_PUBLIC,您的错误消息中提到了该 token ),因此这不是一个合适的选择。

请注意,这与Laravel没有关系。

关于PHP命名空间 'Public',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38961507/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com