与作曲家自定义名称空间中的自动加载嵌套文件夹类

发布于 2025-01-28 11:23:54 字数 1510 浏览 2 评论 0原文

我有这些目录和文件结构。

我想在模型名称空间中加载我的 wtf类

实际上,我想将一些提供商添加到我的项目中,并想将它们定位在自己的文件夹中以进行分类,但要用一个 namespace 将其称为

可能吗?

.
├── composer.json
├── index.php
├── src
│   ├── Controllers
│   │   └── GameController.php
│   ├── Game
│   │   └── Game.php
│   ├── Models
│   │   ├── Game.php
│   │   └── Wtf
│   │       └── Wtf.php
│   └── Player
│       └── Player.php
└── vendor
    ├── autoload.php
    └── composer
        ├── autoload_classmap.php
        ├── autoload_namespaces.php
        ├── autoload_psr4.php
        ├── autoload_real.php
        ├── autoload_static.php
        ├── ClassLoader.php
        └── LICENSE

错误

[500]: GET / - Uncaught Error: Class 'Rp76\Models\Wtf' not found in /home/reza/www/wtf/index.php:13
Stack trace:
#0 {main}
  thrown in /home/reza/www/wtf/index.php on line 13

composer.json

{
  "autoload": {
    "psr-4": {
      "Rp76\\": "src/"
    }
  }
}

index.php

<?php

use Rp76\Game\Game;
use Rp76\Controllers\GameController;
use Rp76\Player\Player;

require_once "vendor/autoload.php";

new Game();
new Player();
new GameController();
new \Rp76\Models\Game();
new \Rp76\Models\Wtf();

wtf.php

<?php

namespace Rp76\Models;

class Wtf
{
    public function __construct()
    {
        echo 'Wtf<br/>';
    }
}

i have these directory and file structure.

i want to load my Wtf class in Models namespace.

in fact i want to add some providers to my project and want to locate them in own folder for classify them but call them with one namespace

its possible?

.
├── composer.json
├── index.php
├── src
│   ├── Controllers
│   │   └── GameController.php
│   ├── Game
│   │   └── Game.php
│   ├── Models
│   │   ├── Game.php
│   │   └── Wtf
│   │       └── Wtf.php
│   └── Player
│       └── Player.php
└── vendor
    ├── autoload.php
    └── composer
        ├── autoload_classmap.php
        ├── autoload_namespaces.php
        ├── autoload_psr4.php
        ├── autoload_real.php
        ├── autoload_static.php
        ├── ClassLoader.php
        └── LICENSE

Error

[500]: GET / - Uncaught Error: Class 'Rp76\Models\Wtf' not found in /home/reza/www/wtf/index.php:13
Stack trace:
#0 {main}
  thrown in /home/reza/www/wtf/index.php on line 13

composer.json

{
  "autoload": {
    "psr-4": {
      "Rp76\\": "src/"
    }
  }
}

index.php

<?php

use Rp76\Game\Game;
use Rp76\Controllers\GameController;
use Rp76\Player\Player;

require_once "vendor/autoload.php";

new Game();
new Player();
new GameController();
new \Rp76\Models\Game();
new \Rp76\Models\Wtf();

Wtf.php

<?php

namespace Rp76\Models;

class Wtf
{
    public function __construct()
    {
        echo 'Wtf<br/>';
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文