So I have a directory that mirrors my namespace structure. The autoloader is correctly loading the file containing my class. the use clause is specifying the correct class with an alias. Despite all of this, PHP says it cannot find the class. I have traditionally not used namespaces, because this always happens to me. But I am trying to force myself to use good practice. So, I am here to try and understand why I can't get this to work.
I have checked that the autoloader has in fact loaded the file. I have checked that the path used by the autoloader is correct.
my class file:
<?php
namespace classes\helpers\core_helper;
class core_helper
{
public static function create_arg_pairs($arguments)
{
.....
}
}//end core_helper
?>
My Main app file:
<?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);
?>
The resulting error is:
Fatal error: Uncaught Error: Class 'classes\helpers\core_helper' not found in /var/www/html/auth/index.php:51 Stack trace: #0 {main} thrown in /var/www/html/auth/index.php on line 51
However I know that the file containing that class was loaded, so the correct namespace was passed to the autoloader and it was converted correctly to the correct path. So why can't I see the class?
By saying
namespace classes\helpers\core_helper;
and then
class core_helper
you are telling the system that your actual class is classes\helpers\core_helper\core_helper
. I expect that what you really want is namespace classes\helpers;
.