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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。