douluo2930 2015-12-28 21:37
浏览 357
已采纳

使用psr-4自动加载文件时出现问题

I've been trying to get psr-4 autoloading work for over a week now with no success.

My file structure is as follows:

-Project
  -src
    -classes
       session.php
  -vendor
  index.php

I've created the psr-4 autoload function as follows:

 "autoload": {
        "psr-4": {
         "classes\\": "src/classes"
    }

}

after using composer dump-autoload -0 ,inside my session.php class I gave the namespace:

namespace classes;

class session{

public static function exist($name){

    return(isset($_SESSION[$name])) ? true : false;
}

I then required the autoloader and used the use function to name the session class as follows:

use src\classes\session as session;

require_once('vendor/autoload.php');

session::put('test', 'test');

after opening up the index.php page, I get a

Fatal error: Class 'src\classes\session' not found in /var/www/test/Project/index.php on line 10

is my directory structure / php correct? I've tried many different guides online and can't seem to get it to work.

  • 写回答

2条回答 默认 最新

  • doujingxi3356 2015-12-28 22:20
    关注

    Most simple solution:

    use classes\session;
    
    require_once('vendor/autoload.php');
    
    session::put('test', 'test');
    

    Unrelated

    However, you probably don't want to use classes as a vendor namespace, but instead adjust a few things here and there:

    Directory structure

    -Project
      -src
        Session.php
      -public
        index.php
      -vendor
    

    Autoloading configuration in composer.json

    {
        "autoload": {
            "psr-4": {
                "Juakali\\": "src"
            }
        }
    }
    

    Replace Juakali with a vendor namespace you prefer, this is just a suggestion. Ideally, if you intend to publish your package, it should be one that isn't already claimed by someone else, see https://packagist.org.

    For reference, see

    Juakali\Session

    Use the aforementioned vendor namespace of your choice:

    namespace Juakali;
    
    class Session
    {
        public static function exist($name)
        {
            return isset($_SESSION[$name]);
        }
    }
    

    Consider using a widely used coding style, for example PSR-2.

    For reference, see

    index.php

    Assuming that you want to expose index.php as the entry point for a web application, move it into a directory which you feel confident to expose as a document root of your web server, adjust the import in index.php, as well as the path to vendor/autoload.php:

    use Juakali\Session;
    
    require_once __DIR__ . '/../vendor/autoload.php';
    
    Session::put('test', 'test');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名