【问题标题】:Composer Uncaught Error: Class 'Instasent\SMSCounter\SMSCounter' not foundComposer 未捕获错误:找不到类“Instasent\SMSCounter\SMSCounter”
【发布时间】:2021-06-15 11:45:56
【问题描述】:

我有文件夹/home/wira/wirdev/smscounter, 在那个文件夹中,我正在从https://github.com/instasent/sms-counter-php 安装短信计数器,安装完成。现在我的文件夹里面是这样的:

.
├── checkSMSSegment.php
├── composer.json
├── composer.lock
├── sms.txt
├── tesSMSLength.php
└── vendor
    ├── autoload.php
    ├── composer
    │   ├── autoload_classmap.php
    │   ├── autoload_namespaces.php
    │   ├── autoload_psr4.php
    │   ├── autoload_real.php
    │   ├── autoload_static.php
    │   ├── ClassLoader.php
    │   ├── installed.json
    │   └── LICENSE
    └── instasent
        └── sms-counter-php
            ├── composer.json
            ├── LICENSE-MIT
            ├── phpunit.xml.dist
            ├── README.md
            ├── SMSCounter.php
            └── Tests
                └── SMSCounterTest.php

这是我要运行的代码 (tesSMSLength.php):

<?php
use Instasent\SMSCounter\SMSCounter;

$text = file_get_contents(dirname(__FILE__)."/sms.txt");

$smsCounter = new SMSCounter();
$result = $smsCounter->count($text);

print_r($result);

现在我想通过键入 sudo php tesSMSLength.php 来使用这个库运行我的代码,但它显示 ERROR class not found 像这样:

[wira@DevWira smscounter]$ sudo php tesSMSLength.php
PHP Fatal error:  Uncaught Error: Class 'Instasent\SMSCounter\SMSCounter' not found in /home/wira/wirdev/smscounter/tesSMSLength.php:6
Stack trace:
#0 {main}
  thrown in /home/wira/wirdev/smscounter/tesSMSLength.php on line 6

我在这里缺少什么?

这是composer.json 内容:

{
    "require": {
       "instasent/sms-counter-php": "^0.4"
    },
    "autoload": {
        "psr-4": {
            "Instasent\\SMSCounter\\": ""
        }
    },
}

【问题讨论】:

  • 您忘记将自动加载器导入到您的测试文件中

标签: php installation composer-php


【解决方案1】:

如果您的代码是 CLI 脚本,则您缺少文件顶部的自动加载导入:

<?php

require 'vendor/autoload.php';

use Instasent\SMSCounter\SMSCounter;

...

此外,由于instasent/sms-counter-php 已经在其composer.json 文件中包含autoload 配置,因此您不必自己将其包含在您的composer.json 中。

【讨论】:

    猜你喜欢
    • 2018-05-27
    • 2020-10-02
    • 2020-11-10
    • 2021-12-03
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    相关资源
    最近更新 更多