PHP autoload debugging with and without composer

305 views Asked by At

Having spent more than 2 hours trying to debug an autoload issue in PHP that was caused by a spelling mistake.

Is there a way to show what file PHP will look for when trying to load a particular class? Looking for something that will take into account the namespaces of the current file and any spl_autoload / composer settings.

So something like the following can be inserted into a script to help debugging :

var_dump(showAutoLoadPaths('myspace\Controller\fancyClass'));
$template = new myspace\fancyClass;

and get an output like :

array (
    0 => 'src/myspace/Controller/fancyClass',
)
Undefined class : fancyClass

allowing my to run :

ls -la src/myspace/Controller/fancyClass
ls: cannot access 'Controller': No such file or directory
ls -la src/myspace
drwxrwxr-x 1 user user 74 May 21 15:47 Controllers

As can be seen from the above, PHP is looking for a folder 'Controller' but the folder is actually called 'Controllers'!

I have tried to interpret the output from spl_autoload_functions() but i cant work out what is what in the file. Any suggestions for existing/built in solutions would be great, but if none exist, any hints on decoding the spl_autoload_functions output so i can write something myself.

1

There are 1 answers

0
William Prigol Lopes On

You can see more info about autoloaders in PSR-0 and PSR-4 patterns.

Basically, if you have autoload folder registered, they will call spl_autoload_register() function so, you can try search inside your directory if you find something with this function and get a debug

Look here about autoload patterns. You probably have an autoload() function that you can check the folders that are set.

And look here to see more about the autoloader.