I tried to load a utility class to my main class with a namespace but everytime I try to run it it seems to not be working as in not finding the class. The error I got is:
Array
(
[0] => C:xampphtdocsProjectPapaassetsphpClassesVerification.php
[1] => C:xampphtdocsProjectPapaassetsphpautoload.php
)
PHP Fatal error: Uncaught Error: Class "VerificationVerificationUtility" not found in C:xampphtdocsProjectPapaassetsphpClassesVerification.php:7
Stack trace:
#0 {main}
thrown in C:xampphtdocsProjectPapaassetsphpClassesVerification.php on line 7
Fatal error: Uncaught Error: Class "VerificationVerificationUtility" not found in C:xampphtdocsProjectPapaassetsphpClassesVerification.php:7
Stack trace:
#0 {main}
thrown in C:xampphtdocsProjectPapaassetsphpClassesVerification.php on line 7
The first array is for me to check what files is included using the get_included_file()
function and I have loaded my autoload.php
file which loads the rest of my utility classes. This is my code that throws the error above:
<?php
namespace Verification;
include dirname(__DIR__) . "\autoload.php";
use VerificationVerificationUtility as utility;
echo (new utility())->test();
print_r(get_included_files());
and my autoload.php
file:
<?php
foreach (glob("Classes/*Utility.php") as $filename)
{
include_once $filename;
}
print_r(get_included_files());
I tried searching online but I cannot find a solution. I tried php namespace Class not found or PHP namespace confusion, class not found
I made changes and double checked my code, I am including the utility class from autoload.php
as this is the output of autoload.php
$ php autoload.php
Array
(
[0] => C:xampphtdocsProjectPapaassetsphpautoload.php
[1] => C:xampphtdocsProjectPapaassetsphpClassesVerificationUtility.php
)
Is my autoloading somehow wrong? Thanks for any help in advance.
Edit: I tried to include the file only without the autoload and it is working just fine. However I would like to use the autoloader because if my classes start expanding it would be a pain to include every single file.
Advertisement
Answer
It looks like the pattern in glob
is relative to the working directory, you may use a determined path.
glob(dirname(__DIR__) . "/Classes/*Utility.php")