@ PHP 注释中的符号被读取为代码?

@ Symbols in PHP comments being read as code?

所以我开始使用这个小库来创建 RESTful PHP 服务器,对 here

在代码中,我注意到注释似乎很重要,换句话说,如果我更改注释,它实际上会更改代码的行为。这是正常做法吗?我以前从未见过这种用法,不忽略评论对我来说似乎很奇怪。

class TestController
{
    /**
     * Returns a JSON string object to the browser when hitting the root of the domain
     *
     * @url GET /
     */
    public function test()
    {
        return "Hello World";
    }
    /**
     * Logs in a user with the given username and password POSTed. Though true
     * REST doesn't believe in sessions, it is often desirable for an AJAX server.
     *
     * @url POST /login
     */
    public function login()
    {
        $username = $_POST['username'];
        $password = $_POST['password']; //@todo remove since it is not needed anywhere
        return array("success" => "Logged in " . $username);
    }
    /**
     * Gets the user by id or current user
     *
     * @url GET /users/$id
     * @url GET /users/current
     */
    public function getUser($id = null)
    {
        // if ($id) {
        //     $user = User::load($id); // possible user loading method
        // } else {
        //     $user = $_SESSION['user'];
        // }
        return array("id" => $id, "name" => null); // serializes object into JSON
    }

基本上,@url 块实际上定义了哪些请求类型,哪些 URL 调用它们下面的函数。这个的范围是什么,它必须是函数正上方的@lines吗?这是标准PHP实践吗?

嗯...是也不是!

不,从某种意义上说,它不是一个正常的 PHP 功能。在 PHP 中,评论就是评论,PHP 不会尝试解析其内容。

是的,因为 PHP 不会解析注释,所以开发人员有时会使用它作为存储库数据的地方。 Symfony 框架就是一个很好的例子。

在这种情况下,您安装的库正在解析 class RestServer.php 本身中的注释。您可以自己阅读 class,尽管其中有一些非常核心的 PHP 和正则表达式。

是PHP博士。参见 https://phpdoc.org/docs/latest/guides/docblocks.html and specifically https://phpdoc.org/docs/latest/guides/docblocks.html#tags

A tag always starts on a new line with an at-sign (@) followed by the name of the tag. Between the start of the line and the tag’s name (including at-sign) there may be one or more spaces or tabs.