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

问题描述 投票:0回答:1

所以我有一个镜像我的命名空间结构的目录。自动装带器正在加载包含我的类的文件。 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'堆栈跟踪:#var {www / html / auth /中抛出#0 {main}第51行的index.php

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

php namespaces autoloader
1个回答
1
投票

通过说

namespace classes\helpers\core_helper;

然后

class core_helper

你告诉系统你的实际课程是classes\helpers\core_helper\core_helper。我希望你真正想要的是namespace classes\helpers;

© www.soinside.com 2019 - 2024. All rights reserved.