即使自动加载器已加载文件,也无法找到类

因此,我有一个反映我的命名空间结构的目录。自动加载器正在正确加载包含我的课程的文件。use子句使用别名指定正确的类。尽管如此,PHP说它找不到该类。传统上,我没有使用过名称空间,因为这总是发生在我身上。但是我试图强迫自己使用良好的做法。因此,我在这里尝试了解为什么我无法使它正常工作。


我检查了自动加载器是否实际上已经加载了文件。我检查了自动装带器使用的路径是否正确。


我的班级档案:


<?php

namespace classes\helpers\core_helper;


class core_helper

{



  public static function create_arg_pairs($arguments)

  {

  .....

  }



}//end core_helper



?>  

我的主应用程序文件:


<?php


ini_set("display_errors", "1");


define('AUTH_ROOT', dirname(__FILE__));


use classes\helpers\core_helper as hlpr;



function autoloader($class)

{


  if(require_once AUTH_ROOT.'/'.str_replace('\\','/',$class).'.php');


}

spl_autoload_register('autoloader');


......


$temp = explode("/", substr($path['path'], 1));

//get the controller

$contoller = strtolower(array_shift($temp));

//get the method

$method = strtolower(array_shift($temp));


$argArray = hlpr::create_arg_pairs($temp);



?>  

产生的错误是:


致命错误:未捕获的错误:在/var/www/html/auth/index.php:51中找不到类'classes \ helpers \ core_helper':堆栈跟踪:#0 {main}抛出在/ var / www / html / auth /中第51行的index.php


但是我知道包含该类的文件已加载,因此正确的名称空间已传递到自动加载器,并且已正确转换为正确的路径。那我为什么看不到课程呢?


慕妹3242003
浏览 137回答 1
1回答

繁花如伊

通过说namespace&nbsp;classes\helpers\core_helper;然后class&nbsp;core_helper您是在告诉系统您的实际班级是classes\helpers\core_helper\core_helper。我希望您真正想要的是namespace classes\helpers;。
打开App,查看更多内容
随时随地看视频慕课网APP