首页 > 解决方案 > PHP 类使用 composer 中的其他类

问题描述

我制作了一个使用 psr-4 自动加载的类。在这堂课中,我想使用我用 composer 下载的一些库中的类,问题是我似乎无法弄清楚。班级:

<?php
namespace CusTelegram\CusCommand; 
use Telegram\Bot\Actions;
use Telegram\Bot\Commands\Command;
class NewEpisodeCommand extends Command
{
    public function handle($arguments)
    { 
        ...
        $dotenv = new Dotenv\Dotenv(__DIR__ . "/../..");
        $this->replyWithMessage(['text' => __DIR__ . "/../.."]);
        $dotenv->overload();
        $client = new ApiVideo\Client\Client($_ENV["API_EMAIL"], $_ENV["API_KEY"]);
        ...
}

方法句柄是从电报 webhook 调用的,所以我不知道如何对其进行调试,但我 100% 确定当 Dotenv 尝试实例化时它会崩溃。树视图:

/CusTelegram
  /CusCommand
    /NewEpisodeCommand.php (this class)
/bot
  /bot.php
/vendor
...

在 bot php 中,我已经需要自动加载。这个类没有问题,只是我不能在 NewEpisodeCommand 类中使用 DotEnv 和 ApiVideo。机器人.php:

ini_set('memory_limit', '-1');
require_once '../vendor/autoload.php';

use Telegram\Bot\Api;
$telegram = new Api(<token>);
$commands = [CusTelegram\CusCommand\StartCommand::class, CusTelegram\CusCommand\NewEpisodeCommand::class, Telegram\Bot\Commands\HelpCommand::class ];
$telegram->addCommands($commands);

$update = $telegram->commandsHandler(true);

--EDIT-- 我能够打印错误,这就是我得到的:

Fatal error: Uncaught Error: Class 'CusTelegram\CusCommand\Dotenv\Dotenv' not found in /membri/streamapi/CusTelegram/CusCommand/NewEpisodeCommand.php

标签: phpcomposer-phpphp-telegram-bot

解决方案


我能够修复我只需要插入使用路径的错误,例如:

use Dotenv\Dotenv;
use ApiVideo\Client\Client;

推荐阅读