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.

图片转代码服务由CSDN问答提供 功能建议

我一直试图让psr-4自动加载工作超过一周,但没有成功。 </ p>

我的文件结构如下:</ p>

  -Project 
 -src 
 -classes 
 session.php 
  -  vendor 
 index.php 
 </ code> </ pre> 
 
 

我创建了psr-4自动加载功能,如下所示:</ p>

   “autoload”:{
“psr-4”:{
“classes \\”:“src / classes”
} 
 
} 
 </ code> </ pre> 
 
 <  p>在使用composer dump-autoload -0之后,在我的session.php类中,我给了命名空间:</ p> 
 
 
 命名空间类; 
 
class session {
 
public static 函数存在($ name){
 
 return(isset($ _ SESSION [$ name]))?  true:false; 
} 
 </ code> </ pre> 
 
 

然后我需要自动加载器并使用use函数来命名会话类,如下所示:</ p>

 使用src \ classes \ session作为会话; 
 
require_once('vendor / autoload.php'); 
 
session :: put('test','test'); 
 </ 在打开index.php页面后,我得到一个</ p> 
 
 

致命错误:Class'src \ classes \ 在第10行的/var/www/test/Project/index.php中找不到会话</ p> </ blockquote>

我的目录结构/ php是否正确? 我在网上尝试了很多不同的指南,似乎无法让它发挥作用。</ p> </ div>

2条回答 默认 最新

相关推荐 更多相似问题