PSR-4目录结构和命名空间是否具有一组功能? [英] PSR-4 directory structure and namespacing for a set of functions?

查看:207
本文介绍了PSR-4目录结构和命名空间是否具有一组功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些有用的PHP函数.我想为他们创建一个符合PSR-4的存储库,但是我发现了这些指南( 1 2

I have a set of PHP functions that I find useful. I want to create a PSR-4 compliant repository for them, but the guides I have found (1,2,3) seem to talk only about classes for autoloading.

例如,我的文件如下,每个文件具有一个功能:

For instance, my files are as follows, with one function per file:

my_cool_function1.php
my_cool_function2.php
... etc.

如何从它们创建符合PSR-4的库?

How can I create a PSR-4 compliant library from them?

推荐答案

无法找到非类的PSR-4自动加载文件的任何文档的原因,这是因为

The reason you're not able to find any documentation for PSR-4 autoloading files which aren't classes, that's because as the specification states - it's designed for autoloading classes.

直接取自官方规格:

此PSR描述了从文件路径自动加载的规范.它是完全可互操作的,并且除包括PSR-0的任何其他自动加载规范外,还可以使用.该PSR还描述了根据规范自动放置文件的位置.

This PSR describes a specification for autoloading classes from file paths. It is fully interoperable, and can be used in addition to any other autoloading specification, including PSR-0. This PSR also describes where to place files that will be autoloaded according to the specification.

更具体地说;

术语""是指接口特征和其他相似结构.

The term "class" refers to classes, interfaces, traits, and other similar structures.

具有功能的文件实际上不是相似的结构.

A file with functions isn't really a similar structure.

要自动加载这些文件,您需要使用files自动加载:

To autoload those files, you'll need to autoload using files:

"autoload": {
    "files": [
        "src/my_cool_function1.php",
        "src/my_cool_function2.php"
    ],
    "psr-4": {
        "SomeNamespace\\": "src/YourNamespace/"
    }
}

您会发现,psr-4规范通常会映射到名称空间.

You'll notice from this, that the psr-4 spec maps (usually) to a namespace.

这篇关于PSR-4目录结构和命名空间是否具有一组功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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