global variable scope with php include scripts external to Drupal

Events happening in the community are now at Drupal community events on www.drupal.org.
texas-bronius's picture

A Drupal site I'm working on requires that I php-include an external db app for listing records. Outside of the Drupal document root, I can run it just fine, but as soon as I make the php include either from within Drupal or just if it is run within the Drupal folder, none of the global variables are referenced from file to file-- I suspect some apache config somewhere but can't find it.

The application is built up of about a zillion library files with constants, functions, etc. The constants are referenced like you would expect:

function getRecordsCommon() {
   global $SOME_FIELDNAMES, $SOME_TYPE_FIELDNAMES;
   ...
   var_dump($SOME_FIELDNAMES);
}

In the above example, I'd get the values of $SOME_FIELDNAMES in a normal apache folder, but I get NULL from within the Drupal tree.

Oh- here's one more tip: I also have moved the include to without the Drupal folder and have included in a Drupal node (from these externally located scripts), and still no love. There are no include errors, just the global variables are not in scope (it appears).

Any ideas what's going on? I sniffed .htaccess and phpinfo(), and nothing stands out as different between the two.

Comments

texas-bronius's picture

So I've since learned a little more about my situation and have moved files around: Now the custom php script application lives under document root directly (whereas sites/default/files borked, sites/all gave equally effective results. Still troubleshooting, so it lives in docroot).

When I call the custom php include from the browser directly, the subsidiary php include containing constants trickles up its global variables appropriately. When calling the script as an include from within a Drupal node, however, the global variables still are coming up empty/undefined.

texas-bronius's picture

Here is my isolated test exhibiting the same observation:
test_top.php:

<?php
print "At the top!<br/>";
require(
"test_sub.php");
print
$test_var;

function
test() {
  global
$test_var;
  print
$test_var;
  print
"Did it print?";
}

test();
?>

test_sub.php:

<?php
$test_var
= "From sub.";
?>

When I call test_top.php from the browser, I get:

At the top!
From sub.From sub.Did it print?

but from within a Drupal php node, I get just:
At the top!
From sub.Did it print?

(along with all the other Drupal web page around it of course).

Try this...

Silicon.Valet's picture

<?php
// magic
global $test_var;

print
"At the top!<br/>";
require(
"test_sub.php");
print
$test_var;

function
test() {
  global
$test_var;
  print
$test_var;
  print
"Did it print?";
}

test();
?>

Ok!

texas-bronius's picture

Jason, you never cease to amaze me. A little sprinkle of magic, and indeed it works both in my test and in my real need!

So.. Any idea why this is, and is it correct/proper to need to do it this way? Maybe it has to do with the out-of-band php eval parser at the time Drupal is parsing the script?

THANK YOU THANK YOU. I may end up with a string of global var's (hmm.. "global *"? :)), but thus far it looks functional.

If I remember correctly, the

neclimdul's picture

If I remember correctly, the include has the scope of the function calling it. This is why CTools/Panels magic $plugin definitions are able to work. Its sort of impossible to tell from your snippets and description but my guess is that's your problem. It would explain why Silicon.Valet's fix helped you and why you hopefully won't need anything as nasty as a theoretical global *;

PS - Globals are bad as you can see. :P Unfortunate your library hasn't figured that out.

include scope

texas-bronius's picture

..the include has the scope of the function calling it..

Ok this makes the observation and solution make sense.