Good day everyone!

There is a certain form, when changing the values ​​of which jquery works and sends the .ajax request to the php file. in php file - a string that looks for the required fields in sql DB and throws it out to the user.

The problem is this: If you set up a connection to DB pens in the php file to which ajax refers, then everything is fine, if you just include(); there include(); (or require_once(); ) for the file that contains the connection to the DB , then it swears, it gives 500 error, due to the fact that the $db variable (in which the connection to the database lies) is not defined. At the same time errors that he could not connect the file from include(); - no.

The same thing if I try to connect a file with classes and then call this class. Swears on the fact that

"Class 'ClassName' not found in ..."

Although the error that failed to connect the file in which the class lies - no. Several times I specially set the paths and the bad name of the file - then it swears that such a file was not found.

  • If you have decided the question, tick one of the answers. If you have not used any of those who have already been published, write your own and tick it off. - D-side
  • @ D-side and I did the edit, probably the Internet has protupil, now I will do it again. - Dan the Hat
  • You edited by inserting a solution into the question and marking [Решено] in the title. I rolled this edit as completely unnecessary, and explained why unnecessary. - D-side

1 answer 1

include and will not swear if the suppression of noncritical errors is configured, but the require will fall tight, try it.

And do not write parentheses for require and include - these are not functions, but language constructs, they do not need brackets, they only confuse you.

  • require still does not produce a fatal error. even quietly displays echo 'hey'; after itself echo 'hey'; but it is worth referring to the class that is registered in the document that we connect through require - writes that there is no such class - Dan the Hat
  • one
    instead of 'hey' print the path of the included file and then the result of executing file_exists for it (something that eats require). + class definitely does not have a namespace? Is there a compiler type loader in the project? - Lexx918
  • Everything, I realized, I was just a fool and did not write the file paths in the wrong way - I had to write absolute from home .. - Dan the Hat
  • one
    bad idea about writing from home. better from __DIR__ . You know and control the relative location of files, but the absolute path to them from the root is not. therefore, it is better to require __DIR_.'/../foo/bar/file.php' and so on. even better, connect a file with a config once, in which the path to the project root is defined via __DIR__ and stored in a variable or constant, and other files already use this value. and even better to smoke mana in the direction of the autoloader, and forget about require at all. - Lexx918