dpz3471 2019-03-04 09:29
浏览 41
已采纳

尝试使用作曲家自动加载wordpress插件时“找不到类”

I'm writing a plugin and trying to use composer to auto load my classes, but I'm unable to get it working. I know there are a lot of questions on this topic but nothing seems to work for me.

directory structure:

  • plugin-name

    • classes
      • Class_Name.php
    • vendor
    • plugin-name.php
    • composer.json
    • composer.lock

composer.json file:

{
    "require": {
        "katzgrau/klogger": "dev-master"
    },
    "autoload": {
        "psr-4": { "Foo\\": "classes" }
    }
}

plugin-name.php file:

namespace Plugins_Main;

use Foo\Class_Name;

require 'vendor/autoload.php';

class Plugin_Name_Bootstrap{
    public static function run() {
        Class_Name::instance();
    }
 }
add_action('plugins_loaded', array('Plugins_Main\Plugin_Name_Bootstrap', 'run'));

Class_Name.php file:

namespace Foo;

class Class_Name{
    protected static $_instance = null;

    public static function instance(){
        if ( self::$_instance === null ) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }
}

According to the docs this autoloading should work, but I'm still getting

Class 'Foo\Class_Name' not found

I thought maybe it related somehow to the fact that I'm trying to initialize "Class_Name" in plugins_loaded hook, But why would that matter if I'm require vendor/autoload.php much earlier?

  • 写回答

1条回答 默认 最新

  • dpwtr666638 2019-03-04 10:00
    关注

    Needed to require autoload.php with __DIR__:

    from

    require 'vendor/autoload.php';
    

    to

    require __DIR__  . '/vendor/autoload.php';
    

    Thanks Damian Dziaduch for the answer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向