-2

I have a PHP script that uses require_once to load modules. It's run hourly by CRON and was working OK Since adding another module, it still works OK overnight (presumable when the load is low) but frequently fails (crashes) during the day. I added some monitoring code to the functions in the module to see whee it fails and it's definitely where it tries to load the module. In an attempt to ascertain why the script was failing to load the module, I changed the require_once to include_once. For some reason, that seems to have solved the problem - the functions in the module are being executed. Can anyone explain why require_once fails to load the module but include_once succeeds? Has anyone even encountered this problem before?

Mike
  • 1
  • 1
  • 1
    Did you put a `try {} catch($e) {}` arround the `require_once` to get what's wrong ? – Obzi Jul 17 '20 at 12:48
  • 3
    Without seeing all the code involved, no one can say for certain. Its clearly a result of bad code somewhere, as those two simply do not act willy-nilly in nature by themselves. – IncredibleHat Jul 17 '20 at 12:48
  • I would not connect this with the load rather with what this script does. Is this require in a conditional by any chance? Maybe when script runs via cron it processes some record and requires the file to do something. Once it is processed another execution is not requiring the file until some condition is met (new order added or whatever), which happens more frequently during the day. Main difference is that include will only warn about failed inclusion and require will stop execution with fatal error - so i will say your code still doesnt work properly, its just less strict about the issue. – blahy Jul 17 '20 at 13:27
  • The require/include is in this piece of code - spl_autoload_register( function( $class ) { require_once CLASSES_DIR . $class . '.class.php'; } ); As I said, it works fine overnight and was working during the day too. Since adding another class, it still works fine overnight but often crashes during the day at the point the new class is instantiated. Since changing the require_once to include_once, it's been working OK. That was the only change. I know the class module is being included because the class functions are being executed. – Mike Jul 17 '20 at 13:54

1 Answers1

0

The require and include functions do the same task, the small difference is :

  • require will produce a fatal (E_COMPILE_ERROR) level error when the specified file location is invalid or for any error
  • include will generate a warning (E_WARNING) and continue the code execution.

See : Click require and include

So you did not solve the problem, you've just transform the error level from ERROR to WARNING, that's why your script is working.

zak
  • 71
  • 8
  • Fair point but it doesn't explain why the require_once version works when the script runs overnight. – Mike Jul 17 '20 at 14:43
  • @Mike we can't answer you without seeing the script, there can be hundreds of reasons that can cause this – zak Jul 17 '20 at 17:56
  • Thanks for all the suggestions. I presumed the require_once was failing because it couldn't load the module. Changing it to include_once appeared to solve the problem but, as you said (above) it would change a fatal error to a warning. However, as the functions in the module were being executed the problem could not be a failure to load the module. It isn't issuing any warnings either. :( – Mike Jul 20 '20 at 11:38