【问题标题】:Cannot get required class to be found in composer无法在作曲家中找到所需的类
【发布时间】:2017-09-25 14:33:12
【问题描述】:

这里是composer.json

{
  "name": "uci/uci-wp-webauth",
  "description": "Wordpress plugin for enabling WebAuth logging in for UCI users on a site-by-site basis.",
  "type": "project",
  "license": "GPL-3.0",
  "minimimum-stability": "stable",
  "autoload": {
    "psr-4": {
      "": "includes/"
    }
  },
  "require": {
    "uci/uci-lib": "dev-master#9097b4f"
  },
  "repositories": [
    {
      "type": "vcs",
      "url": "git@github.com:username/uci-lib"
    }
  ]
}

目录结构如下:

UCI
    -WebAuth.php

WebAuth 类的结构如下:

namespace UCI;

class WebAuth {}

实现 WebAuth 的类/代码:

namespace WebAuth;

use UCI\WebAuth;

class Handler
{
   public function __construct()
    {
        $this->init();

        $this->webauth = new WebAuth();

由于某种原因 PHP 不断报告:

Fatal error: Uncaught Error: Class 'UCI\WebAuth' not found

在建立一个作曲家项目之前,我已经做过很多次了,但是这个真的让我很难过。

【问题讨论】:

    标签: php namespaces composer-php autoload


    【解决方案1】:

    如果没有指定的命名空间,你不能拥有 PSR-4 自动加载器,所以这基本上是错误的:

    "autoload": {
      "psr-4": {
        "": "includes/"
      }
    },
    

    您必须指定要映射到文件夹的命名空间,但是我宁愿将整个 psr-4 替换为简单的 classmap:

    "autoload": {
      "classmap:: ["includes/"]
    },
    

    【讨论】:

    • 感谢您的提示。不幸的是,这与我的问题无关。
    • 用尝试实例化该类对象的代码更新您的问题
    猜你喜欢
    • 2020-01-27
    • 1970-01-01
    • 2019-08-22
    • 2018-04-05
    • 2017-04-08
    • 2016-01-17
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多