[英]How to use the PSR-4 autoload in my /Classes/ folder?
I've tried multiple PSR-4 loaders, but they either don't work or I can't access the classes from another folder. 我尝试了多个PSR-4加载器,但它们要么不起作用,要么我无法从另一个文件夹访问这些类。
My current folder structure: 我当前的文件夹结构:
- Classes
- 课程
--Config.php
--Config.php
--Session.php
--Session.php
-- Frontend (folder)
- 前端(文件夹)
---Login.php
---的login.php
PSR-4 Autoloader: PSR-4自动加载机:
I tried to load all classes using the PSR-4 autoload register. 我尝试使用PSR-4自动加载寄存器加载所有类。 I modified it slightly to my folder structure.
我稍微修改了一下我的文件夹结构。 I've given all classes the namespace Classes, but the ones in the Frotend folder has the namespace Classes\\Frontend.
我给了所有类命名空间Classes,但Frotend文件夹中的命名空间为Classes \\ Frontend。
spl_autoload_register(function ($class) {
// project-specific namespace prefix
$prefix = 'Classes\\';
// base directory for the namespace prefix
$base_dir = __DIR__ . '/Classes/';
// does the class use the namespace prefix?
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
// no, move to the next registered autoloader
return;
}
// get the relative class name
$relative_class = substr($class, $len);
// replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
// if the file exists, require it
if (file_exists($file)) {
require $file;
}
});
I'm not sure if this has to do with the autoloader, but I also want to call the class from any file wherever it's located. 我不确定这是否与自动加载器有关,但我也想从任何位置调用该类。 So if I have a file in
所以,如果我有一个文件
/Frontend/templates/login-page.php
/Frontend/templates/login-page.php
I want to be able to call the class "Classes\\Frontend\\Login". 我希望能够调用类“Classes \\ Frontend \\ Login”。
Is this possible and how would I do that? 这有可能,我该怎么做?
There are mainly two ways to get it working: The first option is to use an absolute server path (starting with a '/'), to set the base directory for your classes in your autoload function: 主要有两种方法可以使它工作:第一种方法是使用绝对服务器路径(以'/'开头),在自动加载功能中设置类的基本目录:
spl_autoload_register(function ($class) {
$prefix = 'Classes\\';
$base_dir = '/var/www/html/my_project/src/'; // your classes folder
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
return;
}
$relative_class = substr($class, $len);
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
if (file_exists($file)) {
require $file;
}
});
The better solution is, as @Félix suggested, to stick with the __DIR__
constant to keep things relative to your project folder. 正如@Félix建议的那样, 更好的解决方案是坚持使用
__DIR__
常量来保持项目文件夹的相对性。 Absolute paths are brittle between deployments on different servers. 不同服务器上的部署之间的绝对路径很脆弱。
__DIR__
refers to the directory of the file it is used in; __DIR__
指的是它所用文件的目录; in this case it is where you register the autoload function. 在这种情况下,它是您注册自动加载功能的地方。 Starting from this directory, you can navigate to the classes base directory, for example
$base_dir = __DIR__ . '/../../src/;
从此目录开始,您可以导航到类基目录,例如
$base_dir = __DIR__ . '/../../src/;
$base_dir = __DIR__ . '/../../src/;
Don't forget to namespace your classes: 不要忘记命名您的类:
namespace Classes;
class Foo
{
public function test()
{
echo 'Hurray';
}
}
Then use classes like this: 然后使用这样的类:
$foo = new Classes\Foo();
$foo->test();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.