duanlu7223 2018-05-25 13:00
浏览 211

PSR4无法正常工作?

Class not found, apparently. I've tried various things but nothing works.

Composer:

"autoload": {
    "psr-4": {
        "App\\": "application/"
    }
}

File structure: https://i.imgur.com/h9wOEqI.png

<?php

namespace App\Library\Classes;

defined('START') or exit('We couldn\'t process your request right now.');

class Application
{
    private static $libraries = array();

    public static function get($library) {
        if (isset(self::$libraries[$library]) && isset(self::$classes[$library])) {
            return self::$libraries[$library];
        }

        $fixedLibrary = str_replace('.', '/', $library);
        $file = ROOT . '/application/library/classes/' . strtolower($fixedLibrary) . '.php';

        self::$libraries[$library] = $library;

        $declared = get_declared_classes();
        $workingClass = end($declared);

        self::$libraries[$library] = new $workingClass();

        return self::$libraries[$library];
    }
}

?>

Error is on this line:

Application::get('test')->test();

Yet, if I change it to this, it works:

include ROOT . '/application/Library/Application.php';
App\Library\Classes\Application::get('test')->test();
  • 写回答

1条回答 默认 最新

  • dongyanpai2701 2018-05-25 13:43
    关注

    The PSR4 is not built-in part or PHP, you need an implementation of autoloader to use this standard such as provided by the Composer.

    When you install or update depedencies, composer generates the relevant code of autoloading, but you can directly update it by the command dump-autoload, as @jibsteroos said. Next you should explicitly include the file vendor/autoload.php in the entry point of your application.

    Also, error message says about class Application, but you should add the use statement at first:

    use App\Library\Classes\Application;
    
    Application::get('test')->test();
    

    Or use the fully qualified class name (class name with namespace prefix):

    \App\Library\Classes\Application::get('test')->test();
    
    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致