3

I am having some problems using require or require_once in that the code halts execution after the call and I am struggling to find what exactly is causing the problem.

I have checked the file exists and can be read (via file_exists and is_readable) but both of these appear to be true so I am a little stumped.

require_once('file1.php');
print('a');
$file = 'file2.php';
if (!file_exists($file)) { die('file does not exist'); }
if (!is_readable($file)) { die('cannot read file'); }
require_once($file);
print('b');

Above code outputs only 'a' and nothing after.

Another file on the server uses both of the files in question in the same way without problem.

Is there anything else I could check?

Any help would be appreciated.

Edit:

Setting error_reporting(E_ALL) does not change the output.

Changing the file path to a blank file in the same directory changes the output to 'ab', indiciating the require has worked and suggesting their is a problem with file2.php, however another file on the server successfully uses these file in the exact same manner.

Running php -l for file2.php returns no errors.

Commenting out the first require makes no difference.

Edit2:

Problem was to do with a call to class_exists and the change in behaviour in version 5.0 to do with auto loading. The code in question was wrote before upgrade to 5.0.

Mitch Satchwell
  • 4,770
  • 2
  • 24
  • 31

2 Answers2

3

Set error_reporting(E_ALL) and try to include an empty file instead of file2.php.

Maybe file2.php has some bug that halts the flow

dynamic
  • 46,985
  • 55
  • 154
  • 231
  • Thanks for the suggestion. I have updated the question with what happens when I do both of the above. Changing error reporting doesn't seem to make a difference, changing path to an empty file works.. Another file on the server however uses this file successfully. – Mitch Satchwell Sep 27 '12 at 08:44
  • Maybe the other who uses that files set some var used by file2.php while you not – dynamic Sep 27 '12 at 08:46
  • Doesn't seem to be the case, it works with the exact same code at the top of the file. – Mitch Satchwell Sep 27 '12 at 08:50
  • there are many other checks file2.php can do... like from where is executed and who required it... teh same code doesnt' mean anything – dynamic Sep 27 '12 at 08:56
  • You are right, it is something in the file that is causing the problem. Commenting out the entire file allows it to work. Thanks for your help, I will update the question when I discover what exactly the problem is. – Mitch Satchwell Sep 27 '12 at 09:00
  • It's better to open a new question... don't mess everything :D Also take care to +1 and accept answer :D – dynamic Sep 27 '12 at 09:00
  • I meant with the solution, not more questions, heh. Thanks for your help! – Mitch Satchwell Sep 27 '12 at 09:03
  • Maybe adding the semicolon at the end (`error_reporting(E_ALL);`) would help to avoid people running into the syntax error. – jakob.j Aug 22 '21 at 11:27
1

There's probably an error somewhere in your file2.php file. You should enable error reporting:

error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);

The error reporting level in case of failed require_once will be E_COMPILE_ERROR

UPDATE Check whether file1 and file2 define functions with the same name, or have other incompatible logic.

Slayer Birden
  • 3,664
  • 2
  • 22
  • 29
  • Thanks for the suggestion, but another file on the server uses file2 in the exact same way without problems which is a little confusing. file2.php also returns no errors when put through a PHP lint (php -l). – Mitch Satchwell Sep 27 '12 at 08:45
  • Thanks but that cannot be the case as another file (in a different directory) uses both file1 and file2 in the same way. – Mitch Satchwell Sep 27 '12 at 08:49
  • Also commenting out the first require makes no difference. – Mitch Satchwell Sep 27 '12 at 08:52