Search code examples
phprequire-once

PHP require_once ignored unless I error_log the return code?


Ok, most bizarre PHP error I've ever encountered.

I have a Joomla module that has a startup.php which includes several files:

...
require_once(SYS_PATH . 'json.php');
require_once(SYS_PATH . 'utf8.php');
...

On one deployment, this reveals an error that the utf8.php is clashing with another set of utf8 functions from a different module. That part makes sense.

The bizarre unexplained part is that I have a second deployment (the dev site) where this problem is not occurring. After debugging, I determined that the 'utf8.php' is not getting loaded at all. The require_once is simply not happening. BUT, if I try printing the return value of require_once, then it DOES include the file and reveal the exception.

// does NOT run any code in utf8.php
...
require_once(SYS_PATH . 'json.php');
error_log('loading utf8.php');
require_once(SYS_PATH . 'utf8.php');
...

-

// DOES run the code in utf8.php
...
require_once(SYS_PATH . 'json.php');
error_log( 'loading utf8.php' );
error_log( 'require returns ' . require_once(SYS_PATH . 'utf8.php') );
...

-

// does NOT run the code in utf8.php
...
require_once(SYS_PATH . 'json.php');
error_log("before include utf8.php");
$x= '' . require_once(SYS_PATH . 'utf8.php');
error_log($x); // prints "1"
error_log("after include utf8.php");

-

// DOES run the code in utf8.php
...
require_once(SYS_PATH . 'json.php');
error_log("before include utf8.php");
include_once(SYS_PATH . 'utf8.php');
error_log("after include utf8.php");

I literally just add or remove the "error_log" wrapper around the require_once function, and it starts working or stops working.

Does anyone have ANY insight into this??

Also: I have added error_log statements before and after this statement and within this and other required files, and they all show up in the log, and the page loads fine (when the require isn't happening on utf8 module). All file permissions are identical.

Linux 3.10.17 Apache 2.2.20 PHP 5.3.6


Solution

  • Figured it out. It was a plugin called "VQmod", which essentially rewrites source files into a temp directory, applying search/replace on the code. The require_once() statement was getting rewritten with other code. When I changed it enough that the search/replace could no longer apply, my original code would get executed.

    I discovered it by adding a

    log_error("... ".__FILE__)
    

    which revealed it was being executed from within vqmod's cache. i.e. each time I edited the file, vqmod would see my changes and rewrite it into the cache, then execute the cache instead of the original.