0
I’m starting to POO
and SPL
and recently faced the following problem: I have the following file that makes the whole bootstrapping
of my application init.php
, and in it, I do the autoload
of all classes in the directory lib/:
set_include_path(get_include_path()
. PATH_SEPARATOR
. APP_ROOT_PATH
. DIRECTORY_SEPARATOR
. 'lib');
spl_autoload_extensions( ".php" );
spl_autoload_register();
However, it returns an error, always saying that some of the classes I later instated were not found:
/welcome.php - Uncaught Error: Class 'Hash' not found in /var/www/project/welcome.php:15
So, to debug, I made the following code:
spl_autoload_register(function($className){
$className = str_replace('\\', DIRECTORY_SEPARATOR, $className);
$file = $className . ".php";
//echo 'Carregando arquivo ' . $file . PHP_EOL;
require_once $file;
});
And in this way, it works perfectly, all my classes are imported! Someone has witnessed something similar?
Welcome.php
<?php
session_start();
define('APP_ROOT_PATH', dirname(__FILE__));
$configFiles = glob(APP_ROOT_PATH . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . '*.php');
foreach($configFiles as $configFile){
require_once $configFile;
}
set_include_path(get_include_path() . PATH_SEPARATOR . APP_ROOT_PATH . DIRECTORY_SEPARATOR . 'lib');
spl_autoload_extensions(".php");
//spl_autoload_register();
spl_autoload_register(function($className){
$otherClassName = $className;
$className = str_replace('\\', DIRECTORY_SEPARATOR, $className);
$file = $className . ".php";
//echo 'Carregando arquivo ' . $file . ' ' . $otherClassName . PHP_EOL;
require_once $file;
});
//echo get_include_path();
require_once 'functions.php';
require_once 'vendor/autoload.php';
Possible duplicate of namespace com php
– novic
@Virgilionovic followed the concept used in the link you made available, I treated the namespaces to have the same names of their directories and managed to solve other conflicts I faced, but still persist this problem, only
spl_autoload_register();
it doesn’t work, the other way it works!– João Vitor
could post the folders in an image and content
welcome.php
?– novic