Directory error with spl_autoload_register

Asked

Viewed 220 times

1

I’m having an error using the autoloader.

Autoloader.php file:

<?php

function carregarClasses($classe) {
    require_once __DIR__ . '/' . $classe . '.php';
}

spl_autoload_register('carregarClasses');

Error:

Warning: require_once(/home/neowix/public_html/Erp/pages/classes/Conexao.php): failed to open stream: No such file or directory in /home/neowix/public_html/Erp/pages/classes/autoload.php on line 4

Fatal error: require_once(): Failed Opening required '/home/neowix/public_html/Erp/pages/classes/Conexao.php' (include_path='.:/opt/php56/lib/php') in /home/neowix/public_html/Erp/pages/classes/autoload.php on line 4

1 answer

0

The constant __DIR__ returns the current file directory, from the file where script is being executed. In your case, /home/neowix/public_html/erp/pages/classes/autoload.php

When mounting the path to autocharge (autoload) the files containing the classes, the path will be:

/home/neowix/public_html/erp/pages/classes/NomeDaClasse.php

If the path is mounting correctly, make sure that the files have the correct nomenclature. In Linux environment, the nomenclature is case sensitive. Example, if the file exists with the name "conexao.php", it will not be found.

  • Sorry, I don’t understand where the bug is. The filenames are correct. I believe something is missing in line 4 which is: require_once DIR . '/' . $class . '. php';

  • The question is uncertain and depends on N factors, of which I have described 2 of the most obvious. The PHP error message is clear and displays the path you tried to load. Just make sure the path is correct. It is no use to say that it is correct because if it were really correct it would not present this error message. Additionally, check the case sensitive issue, i.e., upper and lower case letters in the file and directory nomenclature. It is well described in the answer. I stress that the problem may have another cause, but I bet on these 2 more logical possibilities.

  • I checked the paths and they are correct. I think the problem is how I am using this require_once to call the file. From what I saw the problem is in: Warning: require_once(/home/neowix/public_html/Erp/pages/classesConexao.php) A bar is missing after the "classes". How do I put this bar in the require_once?

  • I solved it. I was really in trouble. It was .class.php and not only . php in require_once. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.