0

I have a bootstrap script.

bootstrap.php

<?php
  $bootstrapFoo = 'foo';

and a main script.

main.php

<?php
  require_once 'bootstrap.php';
  print $bootstrapFoo; // this is working

  // I include a subscript
  include_once 'subscript.php';

subscript.php

<?php
  print $bootstrapFoo; // this is NOT working.

my solution :

<?php
  global $bootstrapFoo;
  print $bootstrapFoo; // now that's working

Is this the right way ?

ps: no duplicates found.

Siguza
  • 21,155
  • 6
  • 52
  • 89
vdegenne
  • 12,272
  • 14
  • 80
  • 106
  • That code, as given, should work just fine. You're probably mixing in a `function` somewhere. – deceze Apr 13 '15 at 09:57
  • @deceze please can you remove the duplicate thing ? your post doesn't really give a decent solution. i am not using any function if you catch the code I wrote. Hence neither can I use 'use' instruction, nor passing a variable in an included file like a function's argument. the only way i see in my case is the global keyword which you do not recommend, then 'is this the right way?' question I asked seems to be answered but what about in the files' scope scenario and which alternatives are out there ? please remove the duplicate thing, it didn't help me out. thanks – vdegenne Apr 13 '15 at 11:40
  • As I said, it is extremely strange that your code *doesn't* work as is. Is this literally the exact code you're using? – deceze Apr 13 '15 at 11:49
  • @deceze I was in fact using a function somewhere, I guess that's a sign of a code turning spaghetti, thanks. – vdegenne Apr 13 '15 at 11:57

0 Answers0