gpt4 book ai didi

php - PSR-0 自动加载器 : Namespaces and the directory structure

转载 作者:行者123 更新时间:2023-12-04 17:04:26 26 4
gpt4 key购买 nike

PSR-0 自动加载器约定是否需要使用映射到目录结构的命名空间?

function autoload($className)
{
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strrpos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';

require $fileName;
}

最佳答案

PSR-0 需要它。

Each namespace separator is converted to a DIRECTORY_SEPARATOR when loading from the file system.





The fully-qualified namespace and class is suffixed with .php when loading from the file system.



https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md

关于php - PSR-0 自动加载器 : Namespaces and the directory structure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17420703/

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