PHP 未捕获错误:未使用 Composer 自动加载找到类 [英] PHP Uncaught Error: Class not found using composer autoload

查看:32
本文介绍了PHP 未捕获错误:未使用 Composer 自动加载找到类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SymfonyConsole 包编写一个简单的项目,但是我遇到了找不到类的异常:

PHP 致命错误:未捕获的错误:在/home/PhpstormProjects/RVLE/RVLE.php:9 中找不到类ProjectExtractLinkCommand"堆栈跟踪:#0 {主要}在第 9 行的/home/PhpstormProjects/RVLE/RVLE.php 中抛出

我找不到问题所在,有人说自动加载器不是标准的,您应该自己编写.我还更新了 composer 并运行了 composer dump-autoload.

这是我的文件 ->

RVLE.php:

#!/usr/bin/env php<?php需要供应商/自动加载.php";使用 ProjectExtractLinkCommand;使用 SymfonyComponentConsoleApplication;$app = new Application('RVLE', '1.0');$app->add(new ExtractLinkCommand());$app->run();

extractCommand.php:

setName('getLinks')->setDescription('提取给定页面url的所有可用视频链接')->addArgument('url', InputArgument::REQUIRED, '页面链接');}公共函数执行(InputInterface $input, OutputInterface $output){$url = $input->getArgument('url');$output->writeln($url);}}

composer.json:

<代码>{要求": {symfony/控制台":^3.3"},自动加载":{psr-4":{项目\":src/"}}}

这是我的项目结构:

<预><代码>.├── composer.json├── composer.lock├── RVLE.php├── src│ └── extractCommand.php└── 供应商├── autoload.php├── 仓├──作曲家├── psr└── 交响乐

解决方案

我认为你需要将你的文件名与你的类名匹配,所以它应该是 ExtractLinkCommand.php,否则 composer autoloader 获胜找不到.

I'm writing a simple project using SymfonyConsole package, but I got class not found exception:

PHP Fatal error:  Uncaught Error: Class 'ProjectExtractLinkCommand' not found in /home/PhpstormProjects/RVLE/RVLE.php:9
Stack trace:
#0 {main}
  thrown in /home/PhpstormProjects/RVLE/RVLE.php on line 9

I can't find what the problem, somebody says autoloader is not standard and you should write it your own. I also updated composer and ran composer dump-autoload.

Here are my files ->

RVLE.php:

#!/usr/bin/env php
<?php
require 'vendor/autoload.php';

use ProjectExtractLinkCommand;
use SymfonyComponentConsoleApplication;

$app = new Application('RVLE' , '1.0');
$app->add(new ExtractLinkCommand());
$app->run();

extractCommand.php:

<?php namespace Project;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputArgument;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;

class ExtractLinkCommand extends Command
{
    public function configure()
    {
        $this->setName('getLinks')
            ->setDescription('extract all available video links for given page url')
            ->addArgument('url', InputArgument::REQUIRED, 'page link');
    }

    public function execute(InputInterface $input, OutputInterface $output)
    {
        $url = $input->getArgument('url');    
        $output->writeln($url);
    }
}

composer.json:

{
  "require": {
    "symfony/console": "^3.3"
  },
  "autoload": {
    "psr-4": {
      "Project\": "src/"
    }
  }
}

This is my project structure:

.
├── composer.json
├── composer.lock
├── RVLE.php
├── src
│   └── extractCommand.php
└── vendor
    ├── autoload.php
    ├── bin
    ├── composer
    ├── psr
    └── symfony

解决方案

I think you need to match your file name to your class name so it should be ExtractLinkCommand.php, otherwise the composer autoloader won't find it.

这篇关于PHP 未捕获错误:未使用 Composer 自动加载找到类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆