首页 \ 问答 \ 试图通过控制台应用程序了解PSR-4自动加载。(Trying to understand PSR-4 autoloading through a console app. What am I doing wrong?)

试图通过控制台应用程序了解PSR-4自动加载。(Trying to understand PSR-4 autoloading through a console app. What am I doing wrong?)

我试图通过编写一个在控制台上运行的非常基本的示例应用程序来了解PSR-4自动加载。

这是我的应用程序(目录)结构:

app/ -library/auth/Author/AuthorInterface.php -library/auth/Author/Author.php -library/auth/Authorize/ -library/database/ -vendor/composer/ -vendor/autoload.php -composer.json -composer.lock -composer.phar -manager.php

我要做的就是运行manager.php并让它回应Author.php的return语句

AuthorInterface.php:

namespace Vee\Auth\Author;

interface AuthorInterface
{
    public function write();
}

Author.php

namespace Vee\Auth\Author;

class Author implements AuthorInterface
{
    function __construct()
    {
        return "Hello";
    }

    public function write()
    {
        return "Hello.write";
    }
}

manager.php:

require "vendor/autoload.php";

use Vee\Auth\Author;

echo new Author();

composer.json:

{
    "autoload": {
        "psr-4": {
            "Vee\\": "library/"
        }
    },
    "require": {
        "monolog/monolog": "^1.21"
    }
}

以下是我尝试运行manager.php时看到的错误:

$ php manager.php 

Fatal error: Class 'Vee\Auth\Author' not found in /<path-to-app>/manager.php on line 7

我已经运行:

$ php composer.phar update

Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Writing lock file
Generating autoload files

我错过了什么?

编辑:

我在Mac上运行PHP 5.5,如果这有什么区别 -

$ php --version
PHP 5.5.36 (cli) (built: Jun 12 2016 23:47:46) 
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies

I'm trying to understand PSR-4 autoloading by writing a very basic example app that runs on the console.

Here is my app's (directory) structure:

app/ -library/auth/Author/AuthorInterface.php -library/auth/Author/Author.php -library/auth/Authorize/ -library/database/ -vendor/composer/ -vendor/autoload.php -composer.json -composer.lock -composer.phar -manager.php

All I'm trying to do is run manager.php and have it echo a return statement from Author.php

AuthorInterface.php:

namespace Vee\Auth\Author;

interface AuthorInterface
{
    public function write();
}

Author.php

namespace Vee\Auth\Author;

class Author implements AuthorInterface
{
    function __construct()
    {
        return "Hello";
    }

    public function write()
    {
        return "Hello.write";
    }
}

manager.php:

require "vendor/autoload.php";

use Vee\Auth\Author;

echo new Author();

composer.json:

{
    "autoload": {
        "psr-4": {
            "Vee\\": "library/"
        }
    },
    "require": {
        "monolog/monolog": "^1.21"
    }
}

And here's the error that I see when I try to run manager.php:

$ php manager.php 

Fatal error: Class 'Vee\Auth\Author' not found in /<path-to-app>/manager.php on line 7

I have already run:

$ php composer.phar update

Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Writing lock file
Generating autoload files

What am I missing?

EDIT:

I'm running PHP 5.5 on a Mac, if that makes any difference-

$ php --version
PHP 5.5.36 (cli) (built: Jun 12 2016 23:47:46) 
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies

原文:https://stackoverflow.com/questions/39718783
更新时间:2023-01-27 08:01

最满意答案

除非内容中包含“ & ”和“ < ”这样的特殊字符,否则字符串

<![CDATA[xxxxx]]>

的意思完全一样

xxxxx

不同的是,在第二种形式中,“ & ”和“ < ”具有特殊含义,而在CDATA形式中,唯一具有特殊含义的是字符串“ ]]> ”,它充当终止符。

你更复杂的例子:

<![CDATA[<![CDATA[TAX INVOICE]]]]><![CDATA[>]]>

有点噩梦,并且源于懒惰的编程习惯,即在懒惰的环境中将文本包装在CDATA部分。 CDATA节不能嵌套,所以第一个]]>终止第一个<!CDATA[ ,这意味着该字符串等同于

<![CDATA[TAX INVOICE]]>

您可能会认为这反过来等同于

TAX INVOICE

但情况并非如此,因为XML解析器只会解释外部CDATA分隔符,因此它将传递给应用程序的内容是

<![CDATA[TAX INVOICE]]>

Unless there are special characters such as "&" and "<" in the content, the string

<![CDATA[xxxxx]]>

means exactly the same as

xxxxx

The difference is that in the second form, "&" and "<" have a special meaning, while in the CDATA form, the only thing with a special meaning is the string "]]>" which acts as a terminator.

Your more complex example:

<![CDATA[<![CDATA[TAX INVOICE]]]]><![CDATA[>]]>

is a bit of a nightmare, and results from a sloppy programming habit of wrapping text in CDATA sections out of laziness. CDATA sections cannot be nested, so the first ]]> terminates the first <!CDATA[, which means that the string is equivalent to

<![CDATA[TAX INVOICE]]>

You might think that this in turn is equivalent to

TAX INVOICE

but that is not the case, because an XML parser will only interpret the outer CDATA delimiters, and the content it will pass to the application is therefore

<![CDATA[TAX INVOICE]]>

相关问答

更多
  • CDATA部分只是为了方便人类作者,而不是为了程序。 它们的唯一用途是让人类能够轻松地将例如SVG示例代码包含在XHTML页面中,而无需仔细地将每个< 等等。 这对我来说是预期的用途。 不要将生成的文档缩小几个字节,因为您可以使用<而不是< 。 再次从上面取得样本(xhtml中的SVG代码),它使得我可以很容易地检查XHTML文件的源代码,并且只复制粘贴SVG代码而不需要替换< 与< 。 CDATA sections are just for the convenience of human a ...
  • 解析器将解析XML文档中的所有文本。 但CDATA部分中的文本将被解析器忽略。 CDATA - (未解析)字符数据 术语CDATA用于不应由XML解析器解析的文本数据。 像“<”和“&”这样的字符在XML元素中是非法的。 “<”将会产生错误,因为解析器将其解释为新元素的开始。 “&”会产生错误,因为解析器将其解释为字符实体的开始。 一些文字,如JavaScript代码,包含很多“<”或“&”字符。 为了避免错误脚本代码可以定义为CDATA。 解析器忽略CDATA部分内的所有内容。 CDATA部分以“
  • CDATA代表字符数据 ,这意味着这些字符串之间的数据包括可以被解释为XML标记的数据,但不应该是。 CDATA和评论之间的主要区别是: 正如理查德所指出的那样 ,CDATA仍然是文件的一部分,而不是一个评论。 在CDATA中,不能包含字符串]]> ( CDEnd ),而在注释中--无效 。 注释中无法识别参数实体引用。 这意味着从一个格式良好的文档中给出这三个XML片段: